Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Strange behaviour in function overloading!

Reply
Thread Tools

Strange behaviour in function overloading!

 
 
Paulo da Silva
Guest
Posts: n/a
 
      07-02-2007
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);
}
 
Reply With Quote
 
 
 
 
Victor Bazarov
Guest
Posts: n/a
 
      07-02-2007
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..

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask


 
Reply With Quote
 
 
 
 
ilikerps@gmail.com
Guest
Posts: n/a
 
      07-02-2007
On Jul 2, 2:40 pm, Paulo da Silva <(E-Mail Removed)> wrote:
> 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.

 
Reply With Quote
 
shadowman
Guest
Posts: n/a
 
      07-02-2007
http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:
> On Jul 2, 2:40 pm, Paulo da Silva <(E-Mail Removed)> wrote:
>> 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
 
Reply With Quote
 
Paulo da Silva
Guest
Posts: n/a
 
      07-03-2007
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>)'
 
Reply With Quote
 
Robert Bauck Hamar
Guest
Posts: n/a
 
      07-03-2007
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
 
Reply With Quote
 
Paulo da Silva
Guest
Posts: n/a
 
      07-03-2007
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
 
Reply With Quote
 
James Kanze
Guest
Posts: n/a
 
      07-03-2007
On Jul 3, 3:59 am, Paulo da Silva <(E-Mail Removed)> wrote:

[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:(E-Mail Removed)
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

 
Reply With Quote
 
ilikerps@gmail.com
Guest
Posts: n/a
 
      07-03-2007
On Jul 2, 4:15 pm, shadowman <(E-Mail Removed)> wrote:
> (E-Mail Removed) wrote:
> > On Jul 2, 2:40 pm, Paulo da Silva <(E-Mail Removed)> wrote:
> >> 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.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Strange behaviour of input() function (Python 3.2) sunrrrise Python 2 05-22-2011 11:21 PM
strange behaviour w.r.t get function Rahul!! Perl Misc 2 03-30-2011 05:00 PM
debugger behaviour different to execution behaviour Andy Chambers Java 1 05-14-2007 09:51 AM
Strange Behaviour of this function g.ankush1@gmail.com C Programming 23 11-20-2006 07:47 AM
strange function behaviour muser C++ 9 10-14-2003 09:50 AM



Advertisments