473,748 Members | 5,232 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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
TeachingAssiste nt;
The error I get is c:\Documents and
Settings\Tony\k au\cplusplus\te st4\start.cpp(8 ): error C2594: 'initializing'
: ambiguous conversions from 'TeachingAssist ent *' 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 TeachingAssiste nt() 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 TeachingAssiste nt : public Student, public Employee
{
public:
TeachingAssiste nt() {}
};

class GraduateAssiste nt : public TeachingAssiste nt
{
};

int main()
{
Person* p = new TeachingAssiste nt;
cout << p->getName() << endl;
}
Aug 17 '05 #1
2 3465
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
TeachingAssiste nt;
The error I get is c:\Documents and
Settings\Tony\k au\cplusplus\te st4\start.cpp(8 ): error C2594:
'initializing'
ambiguous conversions from 'TeachingAssist ent *' 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 TeachingAssiste nt() but I understand why
so that's is no problem.
Since your 'TeachingAssist ent' (BTW, in English it's spelled slightly
differently: 'TeachingAssist ant') 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 'TeachingAssist ent'.

#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 TeachingAssiste nt : public Student, public Employee
{
public:
TeachingAssiste nt() {}
};

class GraduateAssiste nt : public TeachingAssiste nt
{
};

int main()
{
Person* p = new TeachingAssiste nt;
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
TeachingAssiste nt;
The error I get is c:\Documents and
Settings\Tony\k au\cplusplus\te st4\start.cpp(8 ): error C2594: 'initializing'
: ambiguous conversions from 'TeachingAssist ent *' 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
4336
by: Graham Banks | last post by:
Does using multiple inheritance introduce any more performance overhead than single inheritance?
5
2180
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 multiple inheritance still be avoided? If yes, why multiple inheritance is introducted into C++?
20
10082
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 advance for enlightment ... here's the snippet #!/usr/bin/python
22
23383
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 examples?
47
3640
by: Mark | last post by:
why doesn't .NET support multiple inheritance? I think it's so silly! Cheers, Mark
60
4930
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 'target' programming community herein) to get some community input and verify (or not) the following two statements. Few programmers (3 to7%) UNDERSTAND 'Strategic Functional Migration
4
1844
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: C:\PK\Development\Products\UTCAS\4.0\SRC\MeltPracApplication\Dialog\Composit ionForm.cs(942): Argument '1': cannot convert from 'ref MeltPracData.MeltPracDataComposition' to 'ref MeltPracCommon.IDialogPostData'
16
5428
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 first be know at runtime. But is there some strict definitions that defines runtime code and compile time code that can be used in general?
47
4025
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 support multiple inheritance. In C++ for instance I noticed that the compiler flags an error if you use the "ref" keyword on a class with multiple base classes. This supports the above quote. However, under the "CodeClass2.Bases" property (part...
0
8991
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8831
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9548
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
9325
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9249
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8244
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6796
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4607
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
3
2215
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.