Code::Blocks Forums

User forums => General (but related to Code::Blocks) => Topic started by: MoonKid on April 22, 2006, 02:05:48 pm

Title: do I understand C++ wrong?
Post by: MoonKid on April 22, 2006, 02:05:48 pm
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]


Code
#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++?
Title: Re: do I understand C++ wrong?
Post by: PChris on April 22, 2006, 02:15:58 pm
You have to pass the parameter as well ;)
e.g: Function("Hello");
Title: Re: do I understand C++ wrong?
Post by: MoonKid on April 22, 2006, 02:29:06 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.
Title: Re: do I understand C++ wrong?
Post by: PChris on April 22, 2006, 02:35:49 pm
I don't really understand you...
If there is a function in ClassB, why should the compiler search the function in ClassA?
Title: Re: do I understand C++ wrong?
Post by: MoonKid on April 22, 2006, 02:57:14 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.
Title: Re: do I understand C++ wrong?
Post by: sethjackson on April 22, 2006, 03:16:45 pm
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()?
Title: Re: do I understand C++ wrong?
Post by: MoonKid on April 22, 2006, 03:20:10 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
Title: Re: do I understand C++ wrong?
Post by: thomas on April 22, 2006, 04:02:29 pm
Code
class ClassB : public ClassA
{
    public:
        using ClassA::Function;
        bool Function (const char* str)
        {
            return true;
        }
};
Title: Re: do I understand C++ wrong?
Post by: Ceniza on April 22, 2006, 04:06:36 pm
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):

Code: cpp
class ClassB : public ClassA
{
    public:
        using ClassA::Function;

        bool Function (const char* str)
        {
            return true;
        }
};

Another way is to change your main to this:

Code: cpp
int main()
{
ClassB b;

b.ClassA::Function();

return 0;
}