?
=?ISO-8859-1?Q?IvD=B2?=
During a project I ran into trouble when using multiple inheritance. I
was able to resolve the problem, but was unable to really understand the
reasons for the error.
Here is a short example of the problem:
class BaseA
{
...
public:
bool X(char *s);
...
};
class BaseB
{
...
public:
bool X(int &i);
...
};
class AB : public BaseA, public BaseB
{
... /* Nothing special here, no overloading of X */
};
Now in the main function:
int main(int argc, char *argv[])
{
int MyInt;
AB MyAB;
MyAB.X(MyInt); /* Gives compile error */
return 0;
}
Note: I've added an example source to this post.
In the code all functions are implemented. When the code is compiled I
get the following error:
mi.cpp: In function `int main (int, char **)':
mi.cpp:45: request for member `X' is ambiguous
mi.cpp:33: candidates are: bool BaseB::X (int &)
mi.cpp:25: bool BaseA::X (char *)
It seems that the compiler cannot distinguish between the two X
functions. However, it should be clear that the BaseB::X function needs
to be used. Why can't the compiler determine this?
As I said before, I've solved the problem by putting X functions in the
AB class that invoke the correct X function in the BaseA or BaseB
classes. (Is this the best way to do this?)
Thanks in advance...
IvD²
class BaseA
{
public:
BaseA();
bool X(char *s);
};
class BaseB
{
public:
BaseB();
bool X(int &i);
};
class AB : public BaseA, public BaseB
{
public:
AB();
};
BaseA::BaseA()
{}
bool BaseA::X(char *s)
{
return true;
}
BaseB::BaseB()
{}
bool BaseB::X(int &i)
{
return false;
}
AB::AB() : BaseA(), BaseB()
{}
int main(int argc, char *argv[])
{
int MyInt;
char *MyChar;
AB MyAB;
MyAB.X(MyChar); /* Gives compile error */
return 0;
}
was able to resolve the problem, but was unable to really understand the
reasons for the error.
Here is a short example of the problem:
class BaseA
{
...
public:
bool X(char *s);
...
};
class BaseB
{
...
public:
bool X(int &i);
...
};
class AB : public BaseA, public BaseB
{
... /* Nothing special here, no overloading of X */
};
Now in the main function:
int main(int argc, char *argv[])
{
int MyInt;
AB MyAB;
MyAB.X(MyInt); /* Gives compile error */
return 0;
}
Note: I've added an example source to this post.
In the code all functions are implemented. When the code is compiled I
get the following error:
mi.cpp: In function `int main (int, char **)':
mi.cpp:45: request for member `X' is ambiguous
mi.cpp:33: candidates are: bool BaseB::X (int &)
mi.cpp:25: bool BaseA::X (char *)
It seems that the compiler cannot distinguish between the two X
functions. However, it should be clear that the BaseB::X function needs
to be used. Why can't the compiler determine this?
As I said before, I've solved the problem by putting X functions in the
AB class that invoke the correct X function in the BaseA or BaseB
classes. (Is this the best way to do this?)
Thanks in advance...
IvD²
class BaseA
{
public:
BaseA();
bool X(char *s);
};
class BaseB
{
public:
BaseB();
bool X(int &i);
};
class AB : public BaseA, public BaseB
{
public:
AB();
};
BaseA::BaseA()
{}
bool BaseA::X(char *s)
{
return true;
}
BaseB::BaseB()
{}
bool BaseB::X(int &i)
{
return false;
}
AB::AB() : BaseA(), BaseB()
{}
int main(int argc, char *argv[])
{
int MyInt;
char *MyChar;
AB MyAB;
MyAB.X(MyChar); /* Gives compile error */
return 0;
}