473,387 Members | 1,520 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Strange behaviour in function overloading!

Hi!

Why doesn't this work? If I change the name of the vector toLower for
ex. to toLowerV it works! (GCC)

Thanks.
Paulo

..h
_______________
// Auxiliary functions
class Aux
{ private:

public:
static string &toLower(string &s);
static void toLower(vector<string&vs); // Changing the name works!
};

..cpp
___________________

using namespace std;

string &Aux::toLower(string &s)
{ transform(s.begin(),s.end(),s.begin(),::tolower);
return s;
}

void Aux::toLower(vector<string&vs) // Changing the name works!
{ transform(vs.begin(),vs.end(),vs.begin(),Aux::toLo wer);
}
Jul 2 '07 #1
8 1643
Paulo da Silva wrote:
Hi!

Why doesn't this work?
What clue tells you it doesn't work? An error message? Could you
be so kind as to supply the error message (or is it just too much
to ask)?
If I change the name of the vector toLower for
ex. to toLowerV it works! (GCC)

Thanks.
Paulo

.h
_______________
// Auxiliary functions
class Aux
{ private:

public:
static string &toLower(string &s);
static void toLower(vector<string&vs); // Changing the name
works! };
Neither 'string' nor 'vector' is defined here. Did you include
the necessary headers?
>
.cpp
___________________

using namespace std;

string &Aux::toLower(string &s)
{ transform(s.begin(),s.end(),s.begin(),::tolower);
return s;
}

void Aux::toLower(vector<string&vs) // Changing the name works!
{ transform(vs.begin(),vs.end(),vs.begin(),Aux::toLo wer);
}
This code doesn't compile (because 'string' or 'vector' are undefined).
Please post the COMPLETE code (and the rest needed, see FAQ 5.8).

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Jul 2 '07 #2
On Jul 2, 2:40 pm, Paulo da Silva <psdasil...@esotericaX.ptXwrote:
Hi!

Why doesn't this work? If I change the name of the vector toLower for
ex. to toLowerV it works! (GCC)

Thanks.
Paulo

.h
_______________
// Auxiliary functions
class Aux
{ private:

public:
static string &toLower(string &s);
static void toLower(vector<string&vs); // Changing the name works!

};

I may be wrong, but I don't think you can overload a function with a
different return type. It's true for Java, so it has a good chance of
being the same here. Try to change the return type of the void
function to string and see what happens.

Jul 2 '07 #3
il******@gmail.com wrote:
On Jul 2, 2:40 pm, Paulo da Silva <psdasil...@esotericaX.ptXwrote:
>Hi!

Why doesn't this work? If I change the name of the vector toLower for
ex. to toLowerV it works! (GCC)

Thanks.
Paulo

.h
_______________
// Auxiliary functions
class Aux
{ private:

public:
static string &toLower(string &s);
static void toLower(vector<string&vs); // Changing the name works!

};


I may be wrong, but I don't think you can overload a function with a
different return type. It's true for Java, so it has a good chance of
being the same here. Try to change the return type of the void
function to string and see what happens.
Not exactly. You can specify different return types on overloaded
functions in C++ as long as the parameter list also differs. But you
cannot overload solely on return type
Jul 2 '07 #4
Victor Bazarov escreveu:
Paulo da Silva wrote:
>Hi!

Why doesn't this work?
See the compiler output below.
>
What clue tells you it doesn't work? An error message? Could you
be so kind as to supply the error message (or is it just too much
to ask)?
No.
>
>If I change the name of the vector toLower for
ex. to toLowerV it works! (GCC)

Thanks.
Paulo
.....
Reposting a complete desierable compilable part:
pt2.h
#include <string>
#include <vector>

using namespace std;

// Auxiliary functions
class Aux
{ private:

public:
static string &toLower(string &s);
static void toLower(vector<string&vs); // Changing the name works!
};

pt2.cpp
#include "pt2.h"

string &Aux::toLower(string &s)
{ transform(s.begin(),s.end(),s.begin(),::tolower);
return s;
}

void Aux::toLower(vector<string&vs) // Changing the name works!
{ transform(vs.begin(),vs.end(),vs.begin(),Aux::toLo wer);
}

int main()
{
return 0;
}
This code doesn't compile (because 'string' or 'vector' are undefined).
:-)

No!. This is part of a bigger program. The needed headers are there.
I think the compiler cannot determine which "toLower" to use in the 2nd.
transform.
Is there a way I can force it to use the first?
__________________________________________________ ___

pt2.cpp: In static member function 'static void
Aux::toLower(std::vector<std::basic_string<char, std::char_traits<char>,
std::allocator<char, std::allocator<std::basic_string<char,
std::char_traits<char>, std::allocator<char &)':
pt2.cpp:9: error: no matching function for call to
'transform(__gnu_cxx::__normal_iterator<std::basic _string<char,
std::char_traits<char>, std::allocator<char*,
std::vector<std::basic_string<char, std::char_traits<char>,
std::allocator<char, std::allocator<std::basic_string<char,
std::char_traits<char>, std::allocator<char >,
__gnu_cxx::__normal_iterator<std::basic_string<cha r,
std::char_traits<char>, std::allocator<char*,
std::vector<std::basic_string<char, std::char_traits<char>,
std::allocator<char, std::allocator<std::basic_string<char,
std::char_traits<char>, std::allocator<char >,
__gnu_cxx::__normal_iterator<std::basic_string<cha r,
std::char_traits<char>, std::allocator<char*,
std::vector<std::basic_string<char, std::char_traits<char>,
std::allocator<char, std::allocator<std::basic_string<char,
std::char_traits<char>, std::allocator<char >, <unresolved
overloaded function type>)'
Jul 3 '07 #5
Paulo da Silva wrote:
Victor Bazarov escreveu:
>Paulo da Silva wrote:
>>Hi!

Why doesn't this work?
See the compiler output below.
>>
What clue tells you it doesn't work? An error message? Could you
be so kind as to supply the error message (or is it just too much
to ask)?
No.
>>
>>If I change the name of the vector toLower for
ex. to toLowerV it works! (GCC)

Thanks.
Paulo
....
Reposting a complete desierable compilable part:
pt2.h
#include <string>
#include <vector>

using namespace std;
You should _never_ put this in a header file. You should IMHO _never_
use "using namespace std;" either if you can avoid it. If writing "std::"
is too much, rather include the names "using std::string;", but this too
not in header files.
// Auxiliary functions
class Aux
{ private:

public:
static string &toLower(string &s);
static void toLower(vector<string&vs); // Changing the name works!
};

pt2.cpp
#include "pt2.h"

string &Aux::toLower(string &s)
{ transform(s.begin(),s.end(),s.begin(),::tolower);
return s;
}

void Aux::toLower(vector<string&vs) // Changing the name works!
{ transform(vs.begin(),vs.end(),vs.begin(),Aux::toLo wer);
The problem is that std::transform is declared as:
template <typename Input, typename Output, typename Unary>
Output transform(Input a, Input b, Output o, Unary fun);

The compiler has, with the current standard, no way of knowing that fun must
be able to be called with a string. So Aux::toLower gives the compiler two
overloads. You must select the proper:

transform(vs.begin(), vs.end(), vs.begin(),
static_cast<string& (*)(string&)>(Aux::toLower));

This syntax might look awkward. One commonly uses typedefs to fix this:
typedef string& (*stringfun)(string&);
.... static_cast<stringfun>(Aux::toLower)
}

int main()
{
return 0;
}
--
rbh
Jul 3 '07 #6
Robert Bauck Hamar escreveu:
Paulo da Silva wrote:
>Victor Bazarov escreveu:
>>Paulo da Silva wrote:
....
>using namespace std;

You should _never_ put this in a header file. You should IMHO _never_
use "using namespace std;" either if you can avoid it. If writing "std::"
is too much, rather include the names "using std::string;", but this too
not in header files.
Would you please tell me why if I am writing a small size application
and I have no need to use namespace stuff at all?
....
The compiler has, with the current standard, no way of knowing that fun must
be able to be called with a string. So Aux::toLower gives the compiler two
overloads. You must select the proper:

transform(vs.begin(), vs.end(), vs.begin(),
static_cast<string& (*)(string&)>(Aux::toLower));
That's exactly what I was looking for.
Thank you very much.

Paulo
Jul 3 '07 #7
On Jul 3, 3:59 am, Paulo da Silva <psdasil...@esotericaX.ptXwrote:

[Robert has already given the correct answer for the
compiler error. However...]
pt2.h
string &Aux::toLower(string &s)
{ transform(s.begin(),s.end(),s.begin(),::tolower);
Where are you getting ::tolower from? The only standard place I
know is in <ctype.h(formally, a deprecated header), which you
didn't include. And that function wouldn't be legal here
anyway; it results in undefined behavior, since it cannot
legally be called with a char (the result of dereferencing the
string iterator) on systems where char is signed (i.e. most).
return s;
}
--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

Jul 3 '07 #8
On Jul 2, 4:15 pm, shadowman <shadow...@noemail.comwrote:
ilike...@gmail.com wrote:
On Jul 2, 2:40 pm, Paulo da Silva <psdasil...@esotericaX.ptXwrote:
Hi!
Why doesn't this work? If I change the name of the vector toLower for
ex. to toLowerV it works! (GCC)
Thanks.
Paulo
.h
_______________
// Auxiliary functions
class Aux
{ private:
public:
static string &toLower(string &s);
static void toLower(vector<string&vs); // Changing the name works!
};
I may be wrong, but I don't think you can overload a function with a
different return type. It's true for Java, so it has a good chance of
being the same here. Try to change the return type of the void
function to string and see what happens.

Not exactly. You can specify different return types on overloaded
functions in C++ as long as the parameter list also differs. But you
cannot overload solely on return type
Ah, I see. That makes sense, thanks for telling me.

Jul 3 '07 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Ben | last post by:
Hi all, I'm trying to figure out how how complex map, filter and reduce work based on the following piece of code from http://www-106.ibm.com/developerworks/linux/library/l-prog.html : ...
24
by: brian.bird | last post by:
Can anyone explain the behaviour of python when running this script? >>> def method(n, bits=): .... bits.append(n) .... print bits .... >>> method(1) >>> method(2)
3
by: Bruno van Dooren | last post by:
Hi All, i have some (3) different weird pointer problems that have me stumped. i suspect that the compiler behavior is correct because gcc shows the same results. ...
31
by: DeltaOne | last post by:
#include<stdio.h> typedef struct test{ int i; int j; }test; main(){ test var; var.i=10; var.j=20;
9
by: Karahan Celikel | last post by:
Here are three simple classes: class A { public void DoIt(B b) { DoSomething(b); } public void DoSomething(B b) {
3
by: karthik | last post by:
The * operator behaves in 2 different ways. It is used as the value at address operator as well as the multiplication operator. Does this mean * is overloaded in c?
3
by: Adam Nielsen | last post by:
Hi everyone, I've run into yet another quirk with templates, which IMHO is a somewhat limiting feature of the language. It seems that if you inherit multiple classes, and those classes have...
8
by: Dox33 | last post by:
I ran into a very strange behaviour of raw_input(). I hope somebody can tell me how to fix this. (Or is this a problem in the python source?) I will explain the problem by using 3 examples....
160
by: DiAvOl | last post by:
Hello everyone, Please take a look at the following code: #include <stdio.h> typedef struct person { char name; int age; } Person;
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.