It is a simple problem.
The compiler (mingw32) says
[error]
main.cpp:27: error: no matching function for call to `ClassB::Function()'
main.cpp:17: note: candidates are: bool ClassB::Function(const char*)
[/error]
#include <iostream>
class ClassA
{
public:
bool Function ()
{
return true;
}
};
class ClassB : public ClassA
{
public:
bool Function (const char* str)
{
return true;
}
};
int main()
{
ClassB b;
b.Function();
return 0;
}
I do not understand the compilers problem.
Because A::Function and B:Function has different names because the
signaturs (name+parameters) are different.
Shouldn't it work in iso-c++? Is it a compiler problem or I am wrong
understanding standard c++?
You have to pass the parameter as well ;)
e.g: Function("Hello");
Quote from: PChris on April 22, 2006, 02:15:58 PM
You have to pass the parameter as well ;)
e.g: Function("Hello");
No, I do not want to call ClassB::Function. I want to call ClassA::Function. I think the compiler should know that because of the signature.
I know I could call it like that
b.ClassA::Function()
But the compiler should know it by itsefl.
I don't really understand you...
If there is a function in ClassB, why should the compiler search the function in ClassA?
Quote from: PChris on April 22, 2006, 02:35:49 PM
If there is a function in ClassB, why should the compiler search the function in ClassA?
The compiler generates symbols (names) for functions while compiletime.
"bool ClassA::Function()" is a different name instead of "bool ClassB::Function(const char*)".
Not because of A and B. Because of the signature of the methodes.
A methode name is generated on functionname+parameters, that is what is called "signature of a function".
That is what I have learned about compilers.
The ClassB::Function(const char* str) is different than the ClassA::Function(). You declare ClassB, and then try not passing a param to Fuction(const char* str). You cannot do this since Fuction(const char* str) requires a param. Some C++ guru could answer this better I'm sure, but that is the way the compiler works AFAIK. So why don't you declare ClassA, and call that Function()?
Quote from: sethjackson on April 22, 2006, 03:16:45 PM
So why don't you declare ClassA, and call that Function()?
I want to have an object with a to Function() methodes, one with and one without a parameter.
Logicly (in my framework) they should be in different classes.
I will think about how to design it compilable
class ClassB : public ClassA
{
public:
using ClassA::Function;
bool Function (const char* str)
{
return true;
}
};
Your problem is the compiler hides A::Function and it's natural C++ behavior.
You could have lots of different signatures of Function in ClassA. If you make ClassB inherit from ClassA, ClassB will have all those Function too, but if you add a method in ClassB with the name Function (just what you're doing), every inherited signature of Function will be hidden.
To make ClassA::Function visible in ClassB do this (Thomas was faster):
class ClassB : public ClassA
{
public:
using ClassA::Function;
bool Function (const char* str)
{
return true;
}
};
Another way is to change your main to this:
int main()
{
ClassB b;
b.ClassA::Function();
return 0;
}