473,378 Members | 1,531 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

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;
}
Aug 17 '05 #1
2 3429
Tony Johansson wrote:
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


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
Aug 17 '05 #2
Tony Johansson wrote:
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

Aug 17 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Graham Banks | last post by:
Does using multiple inheritance introduce any more performance overhead than single inheritance?
5
by: Morgan Cheng | last post by:
It seems no pattern defined by GoF takes advantage of multiple inheritance. I am wondering if there is a situation where multiple inheritance is a necessary solution. When coding in C++, should...
20
by: km | last post by:
Hi all, In the following code why am i not able to access class A's object attribute - 'a' ? I wishto extent class D with all the attributes of its base classes. how do i do that ? thanks in...
22
by: Matthew Louden | last post by:
I want to know why C# doesnt support multiple inheritance? But why we can inherit multiple interfaces instead? I know this is the rule, but I dont understand why. Can anyone give me some concrete...
47
by: Mark | last post by:
why doesn't .NET support multiple inheritance? I think it's so silly! Cheers, Mark
60
by: Shawnk | last post by:
Some Sr. colleges and I have had an on going discussion relative to when and if C# will ever support 'true' multiple inheritance. Relevant to this, I wanted to query the C# community (the...
4
by: tony | last post by:
Hello! My question is about calling this method CollectData below but I get a compile error that I shouldn't have because the type parameter is correct. The compile error is the following:...
16
by: desktop | last post by:
I have read that using templates makes types know at compile time and using inheritance the types are first decided at runtime. The use of pointers and casts also indicates that the types will...
47
by: Larry Smith | last post by:
I just read a blurb in MSDN under the C++ "ref" keyword which states that: "Under the CLR object model, only public single inheritance is supported". Does this mean that no .NET class can ever...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.