why compile error in multiple inheritance

T

Tony Johansson

Hello Experts!

I have been playing around a bit with this multiple inheritance and I wonder
why do I get a compile error
if I remove the virtual keyvord from the declaration of the Student class
and the Employee class.
I have removed all kind of data from all classes.
The compile error occur when I instansiate Person* p = new
TeachingAssistent;
The error I get is c:\Documents and
Settings\Tony\kau\cplusplus\test4\start.cpp(8): error C2594: 'initializing'
: ambiguous conversions from 'TeachingAssistent *' to 'Person *'

I know that if I have some data in the virtual base klass and not using the
virtual keyword I would get compile error because of having double defined
data in the TeachingAssistent() but I understand why so that's is no
problem.


#include <string>
using namespace std;

class Person
{
public:
Person() {}
string getName() const
{
return "rulle";
}
};

class Student : public Person
{
public:
Student() {}
};

class Employee : public Person
{
public:
Employee() {}
};

class TeachingAssistent : public Student, public Employee
{
public:
TeachingAssistent() {}
};

class GraduateAssistent : public TeachingAssistent
{
};

int main()
{
Person* p = new TeachingAssistent;
cout << p->getName() << endl;
}
 
V

Victor Bazarov

Tony said:
I have been playing around a bit with this multiple inheritance and I
wonder why do I get a compile error
if I remove the virtual keyvord from the declaration of the Student
class and the Employee class.
I have removed all kind of data from all classes.
The compile error occur when I instansiate Person* p = new
TeachingAssistent;
The error I get is c:\Documents and
Settings\Tony\kau\cplusplus\test4\start.cpp(8): error C2594:
'initializing'

I know that if I have some data in the virtual base klass and not
using the virtual keyword

How could you have a virtual base class without using 'virtual' keyword?
I would get compile error because of having
double defined data in the TeachingAssistent() but I understand why
so that's is no problem.

Since your 'TeachingAssistent' (BTW, in English it's spelled slightly
differently: 'TeachingAssistant') has _two_ copies of 'Person' class,
one from 'Student', and the other from 'Employee', the compiler cannot
decide to which 'Person' to convert the pointer to 'TeachingAssistent'.

#include <string>
using namespace std;

class Person
{
public:
Person() {}
string getName() const
{
return "rulle";
}
};

class Student : public Person
{
public:
Student() {}
};

class Employee : public Person
{
public:
Employee() {}
};

class TeachingAssistent : public Student, public Employee
{
public:
TeachingAssistent() {}
};

class GraduateAssistent : public TeachingAssistent
{
};

int main()
{
Person* p = new TeachingAssistent;
cout << p->getName() << endl;
}

V
 
R

Robben

Tony said:
Hello Experts!

I have been playing around a bit with this multiple inheritance and I wonder
why do I get a compile error
if I remove the virtual keyvord from the declaration of the Student class
and the Employee class.
I have removed all kind of data from all classes.
The compile error occur when I instansiate Person* p = new
TeachingAssistent;
The error I get is c:\Documents and
Settings\Tony\kau\cplusplus\test4\start.cpp(8): error C2594: 'initializing'
: ambiguous conversions from 'TeachingAssistent *' to 'Person *'
For more information take a look at virtual base class documentation.
For the time-being you can fix the problem
as shown below
class Student : public Person
change to class Student : virtual public person
class Employee : public Person
change to class Employee : virtual public person
 

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,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top