Help | Site Map
Connecting Tech Pros Worldwide
 
 
LinkBack Thread Tools
  #1  
Old August 17th, 2005, 01:05 AM
Tony Johansson
Guest
 
Posts: n/a
Default why compile error in multiple inheritance

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;
}


  #2  
Old August 17th, 2005, 01:25 AM
Victor Bazarov
Guest
 
Posts: n/a
Default Re: why compile error in multiple inheritance

Tony Johansson wrote:[color=blue]
> 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'[color=green]
>> ambiguous conversions from 'TeachingAssistent *' to 'Person *'[/color]
>
> I know that if I have some data in the virtual base klass and not
> using the virtual keyword[/color]

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

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'.

[color=blue]
> #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;
> }[/color]

V


  #3  
Old August 17th, 2005, 02:15 AM
Robben
Guest
 
Posts: n/a
Default Re: why compile error in multiple inheritance

Tony Johansson wrote:[color=blue]
> 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 *'
>[/color]
For more information take a look at virtual base class documentation.
For the time-being you can fix the problem
as shown below[color=blue]
>
>
> class Student : public Person[/color]
change to class Student : virtual public person[color=blue]
>
> class Employee : public Person[/color]
change to class Employee : virtual public person

 

Bookmarks

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over network members.
Post your question now . . .
It's fast and it's free

Popular Articles