Call of overloaded derived method problem

L

Lagarde s?bastien

Hello,

I have the following code

struct Toto
{
void Add(int x){}
};

struct Titi : public Toto
{
void Add(int x, int y){}
};

int main ()
{
Titi titi;
titi.Add(1);
}

and i get the following error msg :
main.cpp(19): error C2660: 'Titi::Add' : function does not take 1 parameters

if i change the code "titi.Add(1);" to "titi.Toto::Add(1);"
There is no more problem.

Is this a specific C++ behavior ? or my compiler (msvc7) ?
Is there a good way to write "titi.Add(1)".

I know i can wrap "Add" in Titi class and call Add Parent but
if i can avoid this...

Thanks.
 
K

Karl Heinz Buchegger

Lagarde said:
Hello,

I have the following code

struct Toto
{
void Add(int x){}
};

struct Titi : public Toto
{
void Add(int x, int y){}
};

int main ()
{
Titi titi;
titi.Add(1);
}

and i get the following error msg :
main.cpp(19): error C2660: 'Titi::Add' : function does not take 1 parameters

if i change the code "titi.Add(1);" to "titi.Toto::Add(1);"
There is no more problem.

Is this a specific C++ behavior ?
Yes.

or my compiler (msvc7) ?

No. Your compiler is correct.
The Add function in Titi hides the Add function in Toto
Is there a good way to write "titi.Add(1)".

struct Titi : public Toto
{
using Toto::Add;

void Add( int x, int y );
};
 
S

Sreenivas M

Lagarde s?bastien said:
Hello,

I have the following code

struct Toto
{
void Add(int x){}
};

struct Titi : public Toto
{
void Add(int x, int y){}
};

int main ()
{
Titi titi;
titi.Add(1);
}

and i get the following error msg :
main.cpp(19): error C2660: 'Titi::Add' : function does not take 1 parameters

if i change the code "titi.Add(1);" to "titi.Toto::Add(1);"
There is no more problem.

Is this a specific C++ behavior ? or my compiler (msvc7) ?
Is there a good way to write "titi.Add(1)".

I know i can wrap "Add" in Titi class and call Add Parent but
if i can avoid this...

Thanks.

Hello,

Overloading works only in the same scope..
The methods are not overloded if they are in different scope.
The compiler would issue an error, as it cannot find the other Add method.

Thanx.
sreeni
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top