What's Wrong with this code

D

Dhirendra Singh

class TestClass1
{
public:
int func1(int i, int j) { return 0; }
};

class TestClass2 : public TestClass1
{
public:
int func1(int i);
};

int TestClass2::func1(int i)
{
this->func1(i,1);
return 0;
}

When i compile it i get the error "too many arguments in function call
this->func1(i,1); "
when i change the name of the function func1 to func2 in TestClass2 it
compiles successfully.
I do not understand why ?
 
D

David Harmon

On Wed, 14 Nov 2007 16:25:40 -0800 in comp.lang.c++, Dhirendra Singh
int TestClass2::func1(int i)
{
this->func1(i,1);
return 0;
}

When i compile it i get the error "too many arguments in function call
this->func1(i,1); "
when i change the name of the function func1 to func2 in TestClass2 it
compiles successfully.

TestClass2::func1 hides TestClass1::func1

You could say
using TestClass1::func1;

This issue is covered in Marshall Cline's C++ FAQ. See the topic "[23.9]
What's the meaning of, Warning: Derived::f(char) hides Base::f(double)?"
It is always good to check the FAQ before posting. You can get the FAQ
at:
http://www.parashift.com/c++-faq-lite/
 
M

Marc G

Dhirendra Singh said:
class TestClass1
{
public:
int func1(int i, int j) { return 0; }
};

class TestClass2 : public TestClass1
{
public:
int func1(int i);
};

int TestClass2::func1(int i)
{
this->func1(i,1);
return 0;
}

write
int TestClass2::func1(int i)
{
/* this->* is not nécessary .../
TestClass1::func1(i,1);
return 0;
}
 
D

Dhirendra Singh

"Dhirendra Singh" <[email protected]> a écrit dans le message de







write
int TestClass2::func1(int i)
{
/* this->* is not nécessary .../
TestClass1::func1(i,1);
return 0;
}- Hide quoted text -

- Show quoted text -

I understand writing TestClass1::func1(i,1); will work.
but i do not understand why the above code do not compile.
func1(int i) and func2(int i, int j) are not different functions ???
if it is different then the call to func1(i,1) should resolve to
TestClass1::func1(i,1) by compiler.
 
K

Kai-Uwe Bux

Dhirendra said:
I understand writing TestClass1::func1(i,1); will work.
but i do not understand why the above code do not compile.
func1(int i) and func2(int i, int j) are not different functions ???
if it is different then the call to func1(i,1) should resolve to
TestClass1::func1(i,1) by compiler.

Member functions hide names from base classes. You could use explicit
qualification or a using directive to get around it.


Best

Kai-Uwe Bux
 
R

Ron Natalie

Kai-Uwe Bux said:
Member functions hide names from base classes. You could use explicit
qualification or a using directive to get around it.
To be complete:

Any name defined in a derived class hides the same name in base classes.

The name is resolved first and then overloads for THAT name are considered.
 

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

No members online now.

Forum statistics

Threads
473,776
Messages
2,569,603
Members
45,197
Latest member
Sean29G025

Latest Threads

Top