[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.c++

Overloading mess: No matching function for call

tomas

9/29/2008 10:01:00 PM

Hi.
I wrote a code similar to this one, for a wrapper application that I
needed:

#include <iostream>

using namespace std;

class Client
{
public:
void setEndpoint(const string& s) throw() { a_endpoint = s;
setEndpoint(); }

protected:
virtual void setEndpoint() throw() = 0;

string a_endpoint;
};

class NotifyClient : public Client
{
public:
const char* endpoint;

private:
void setEndpoint() throw() { endpoint = a_endpoint.c_str(); }
};


int main()
{
NotifyClient client;
string url("http://www.google...);
client.setEndpoint( url );

return 0;
}


But I'm getting this compile error with g++:

inheritanceTopic.cc:30: error: no matching function for call to
`NotifyClient::setEndpoint(std::string&)'
inheritanceTopic.cc:22: note: candidates are: virtual void
NotifyClient::setEndpoint()

If I change the line,

client.setEndpoint( url );

by

client.Client::setEndpoint( url );

it works.

I'm wondering why is it. I guess it has something to do with function
overloading and the compiler, because if I change names, it works too.

Can anyone give me a better explanation?
I would like to use the same name in the functions, but I don't like
the style of:

client.Client::setEndpoint( url );

pretty ugly....

Thanks in advance,
Tomas.
5 Answers

sasha

9/30/2008 12:46:00 AM

0

On Sep 29, 6:00 pm, tomas <tomaso...@gmail.com> wrote:
> Hi.
> I wrote a code similar to this one, for a wrapper application that I
> needed:
>
> #include <iostream>
>
> using namespace std;
>
> class Client
> {
> public:
> void setEndpoint(const string& s) throw() { a_endpoint = s;
> setEndpoint(); }
>
> protected:
> virtual void setEndpoint() throw() = 0;
>
> string a_endpoint;
>
> };
>
> class NotifyClient : public Client
> {
> public:
> const char* endpoint;
>
> private:
> void setEndpoint() throw() { endpoint = a_endpoint.c_str(); }
>
> };
>
> int main()
> {
> NotifyClient client;
> string url("http://www.google...);
> client.setEndpoint( url );
>
> return 0;
>
> }
>
> But I'm getting this compile error with g++:
>
> inheritanceTopic.cc:30: error: no matching function for call to
> `NotifyClient::setEndpoint(std::string&)'
> inheritanceTopic.cc:22: note: candidates are: virtual void
> NotifyClient::setEndpoint()
>
> If I change the line,
>
> client.setEndpoint( url );
>
> by
>
> client.Client::setEndpoint( url );
>
> it works.
>
> I'm wondering why is it. I guess it has something to do with function
> overloading and the compiler, because if I change names, it works too.
>
> Can anyone give me a better explanation?
> I would like to use the same name in the functions, but I don't like
> the style of:
>
> client.Client::setEndpoint( url );
>
> pretty ugly....
>
> Thanks in advance,
> Tomas.

void setEndpoint() in the NotifyClient class is private and in the
Client is protected. You cannot lower the access level of the virtual
function. BTW, visa verse would works.

puzzlecracker

9/30/2008 12:48:00 AM

0

On Sep 29, 6:00 pm, tomas <tomaso...@gmail.com> wrote:
> Hi.
> I wrote a code similar to this one, for a wrapper application that I
> needed:
>
> #include <iostream>
>
> using namespace std;
>
> class Client
> {
> public:
> void setEndpoint(const string& s) throw() { a_endpoint = s;
> setEndpoint(); }
>
> protected:
> virtual void setEndpoint() throw() = 0;
>
> string a_endpoint;
>
> };
>
> class NotifyClient : public Client
> {
> public:
> const char* endpoint;
>
> private:
> void setEndpoint() throw() { endpoint = a_endpoint.c_str(); }
>
> };
>
> int main()
> {
> NotifyClient client;
> string url("http://www.google...);
> client.setEndpoint( url );
>
> return 0;
>
> }
>
> But I'm getting this compile error with g++:
>
> inheritanceTopic.cc:30: error: no matching function for call to
> `NotifyClient::setEndpoint(std::string&)'
> inheritanceTopic.cc:22: note: candidates are: virtual void
> NotifyClient::setEndpoint()
>
> If I change the line,
>
> client.setEndpoint( url );
>
> by
>
> client.Client::setEndpoint( url );
>
> it works.
>
> I'm wondering why is it. I guess it has something to do with function
> overloading and the compiler, because if I change names, it works too.
>
> Can anyone give me a better explanation?
> I would like to use the same name in the functions, but I don't like
> the style of:
>
> client.Client::setEndpoint( url );
>
> pretty ugly....
>
> Thanks in advance,
> Tomas.

void setEndpoint() in the NotifyClient class is private and in the
Client is protected. You cannot lower the access level of the virtual
function. BTW, visa verse would works.

Pete Becker

9/30/2008 12:54:00 AM

0

On 2008-09-29 20:48:16 -0400, puzzlecracker <ironsel2000@gmail.com> said:

> On Sep 29, 6:00 pm, tomas <tomaso...@gmail.com> wrote:
>> Hi.
>> I wrote a code similar to this one, for a wrapper application that I
>> needed:
>>
>> #include <iostream>
>>
>> using namespace std;
>>
>> class Client
>> {
>> public:
>> void setEndpoint(const string& s) throw() { a_endpoint = s;
>> setEndpoint(); }
>>
>> protected:
>> virtual void setEndpoint() throw() = 0;
>>
>> string a_endpoint;
>>
>> };
>>
>> class NotifyClient : public Client
>> {
>> public:
>> const char* endpoint;
>>
>> private:
>> void setEndpoint() throw() { endpoint = a_endpoint.c_str(); }
>>
>> };
>>
>> int main()
>> {
>> NotifyClient client;
>> string url("http://www.google...);
>> client.setEndpoint( url );
>>
>> return 0;
>>
>> }
>>
>> But I'm getting this compile error with g++:
>>
>> inheritanceTopic.cc:30: error: no matching function for call to
>> `NotifyClient::setEndpoint(std::string&)'
>> inheritanceTopic.cc:22: note: candidates are: virtual void
>> NotifyClient::setEndpoint()
>>
>> If I change the line,
>>
>> client.setEndpoint( url );
>>
>> by
>>
>> client.Client::setEndpoint( url );
>>
>> it works.
>>
>> I'm wondering why is it. I guess it has something to do with function
>> overloading and the compiler, because if I change names, it works too.
>>
>> Can anyone give me a better explanation?
>> I would like to use the same name in the functions, but I don't like
>> the style of:
>>
>> client.Client::setEndpoint( url );
>>
>> pretty ugly....
>>
>> Thanks in advance,
>> Tomas.
>
> void setEndpoint() in the NotifyClient class is private and in the
> Client is protected. You cannot lower the access level of the virtual
> function. BTW, visa verse would works.

Both work. You're thinking of Java.

The problem is that overloading only takes place among functions
defined in the same scope. Client::setEndpoint(const string&) and
NotifyClient::setEndpoint() are not defined in the same scope, so they
do not overload. The second one hides the first.

--
Pete
Roundhouse Consulting, Ltd. (www.versatilecoding.com) Author of "The
Standard C++ Library Extensions: a Tutorial and Reference
(www.petebecker.com/tr1book)

Lars Tetzlaff

9/30/2008 12:01:00 PM

0

tomas schrieb:
>
>
> But I'm getting this compile error with g++:
>
> inheritanceTopic.cc:30: error: no matching function for call to
> `NotifyClient::setEndpoint(std::string&)'
> inheritanceTopic.cc:22: note: candidates are: virtual void
> NotifyClient::setEndpoint()
>
>
> Thanks in advance,
> Tomas.

Insert

public:
using Client::setEndpoint;

into class NotifyClient, that makes both functions visible.

Lars

Jirí Palecek

10/4/2008 9:12:00 PM

0

On Tue, 30 Sep 2008 00:00:32 +0200, tomas <tomasorti@gmail.com> wrote:

> Hi.
> I wrote a code similar to this one, for a wrapper application that I
> needed:
>
> #include <iostream>
>
> using namespace std;
>
> class Client
> {
> public:
> void setEndpoint(const string& s) throw() { a_endpoint = s;
> setEndpoint(); }
>
> protected:
> virtual void setEndpoint() throw() = 0;
>
> string a_endpoint;
> };
>
> class NotifyClient : public Client
> {
> public:
> const char* endpoint;
>
> private:
> void setEndpoint() throw() { endpoint = a_endpoint.c_str(); }
> };
>
>
> int main()
> {
> NotifyClient client;
> string url("http://www.google...);
> client.setEndpoint( url );
>
> return 0;
> }
>
>
> But I'm getting this compile error with g++:
>
> inheritanceTopic.cc:30: error: no matching function for call to
> `NotifyClient::setEndpoint(std::string&)'
> inheritanceTopic.cc:22: note: candidates are: virtual void
> NotifyClient::setEndpoint()
>
> If I change the line,
>
> client.setEndpoint( url );
>
> by
>
> client.Client::setEndpoint( url );
>
> it works.
>
> I'm wondering why is it. I guess it has something to do with function
> overloading and the compiler, because if I change names, it works too.
>
> Can anyone give me a better explanation?

This is beacuse you have a NotifyClient class and there is only one
"setEndpoint" function in it - the one with no parameters. The rule is,
any member of a class shadows the members of the same name in its base
classes. There are solutions for this.

1) you can import Client::setEndpoint to NotifyClient via using
declaration. However, this has the caveat that you have to do this for all
derived classes.

2) the simplest and easiest solution is to rename the protected
setEndpoint() to something else - like doSetEndpoint() - and have only
one, public, setEndpoint(string) function in the base class.

Regards
Jiri Palecek