Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Virtual inheritance and multiple files

Reply
Thread Tools

Virtual inheritance and multiple files

 
 
Klaas Vantournhout
Guest
Posts: n/a
 
      11-19-2006
Hi,

Recently I obtained a problem with virtual inheritance when implementing
it in multiple files. To present the problem I have included at the
bottom of this post the code of the 4 files. I show results with the
gnu compiler but i have the same results with the intel compiler.

file1.cpp compiles without a problem, the problem occurs when compiling
file2.cpp. It complains about not finding the constructor of the class
base.

[klaas@blackdwarf cpptest]$ g++ -c -Wall file2.cpp
file2.cpp: In constructor 'doublederived::doublederived(int)':
file2.cpp:3: error: no matching function for call to 'base::base()'
file1.hpp:3: note: candidates are: base::base(int)
file1.hpp:1: note: base::base(const base&)

When derived does not virtual inherit base, this does not give a
problem. Is there a reason why this is not working?

Regards
Klaas

// file1.hpp
class base {
public:
base(int);
};

class derived : public virtual base {
public:
derived(int);
};

// file1.cpp
#include <iostream>
#include "file1.hpp"
base::base(int a) { std::cout << a << std::endl;}
derived::derived(int a) : base(a) { }

// file2.hpp
#include "file1.hpp"
class doublederived : public derived {
public:
doublederived(int);
};

// file2.cpp
#include "file2.hpp"
doublederived::doublederived(int a) : derived(a) { }
 
Reply With Quote
 
 
 
 
BobR
Guest
Posts: n/a
 
      11-20-2006

Klaas Vantournhout wrote in message
<4560e845$0$1120$>...
>Hi,
>
>Recently I obtained a problem with virtual inheritance when implementing
>it in multiple files. To present the problem I have included at the
>bottom of this post the code of the 4 files. I show results with the
>gnu compiler but i have the same results with the intel compiler.
>
>file1.cpp compiles without a problem, the problem occurs when compiling
>file2.cpp. It complains about not finding the constructor of the class
>base.
>
>[klaas@blackdwarf cpptest]$ g++ -c -Wall file2.cpp
>file2.cpp: In constructor 'doublederived::doublederived(int)':
>file2.cpp:3: error: no matching function for call to 'base::base()'
>file1.hpp:3: note: candidates are: base::base(int)
>file1.hpp:1: note: base::base(const base&)
>
>When derived does not virtual inherit base, this does not give a
>problem. Is there a reason why this is not working?
>
>Regards
>Klaas
>
>// file1.hpp
>class base {
> public:
> base(int);
>};
>
>class derived : public virtual base {
> public:
> derived(int);
>};
>
>// file1.cpp
>#include <iostream>
>#include "file1.hpp"
>base::base(int a) { std::cout << a << std::endl;}
>derived::derived(int a) : base(a) { }
>
>// file2.hpp
>#include "file1.hpp"
>class doublederived : public derived {
> public:
> doublederived(int);
>};
>
>// file2.cpp
>#include "file2.hpp"
>doublederived::doublederived(int a) : derived(a) { }


I'm not real sharp on this one. I looked at Eckel's and found this (in
diamond example):

class Left : virtual public Top { public: <snip>
Left(int m, int n) : Top(m) { y = n; }
};

class Right : virtual public Top { public: <snip>
Right(int m, int n) : Top(m) { z = n; }
};

class Bottom : public Left, public Right {
int w;
public:
Bottom(int i, int j, int k, int m)
: Top(i), Left(0, j), Right(0, k) { w = m; }
};

Note how, even though Bottom did not directly derive from Top, it was still
initialised in Bottom.

So, maybe try(?):

doublederived::doublederived(int a) : base(a), derived(a) { }

Makes since to me because your error is in looking for a base default
constructor, instead of using your defined constructor.

I'm just guessing at this, but, it shouldn't hurt to try it.
I tried it (in a single file) and it worked.

--
Bob R
POVrookie


 
Reply With Quote
 
 
 
 
Frank
Guest
Posts: n/a
 
      11-20-2006
I think BobR gave the correct solution. In this case, virtual
inheritance makes the constructor of "base" delay to the most derived
class "'doublederived" and suppresses the call of it from "derived".
Actually, in the constructor of " base", if you don't invoke the
constructor of "base", its default constructor will be the first
candidate.

"BobR д
"
> Klaas Vantournhout wrote in message
> <4560e845$0$1120$>...
> >Hi,
> >
> >Recently I obtained a problem with virtual inheritance when implementing
> >it in multiple files. To present the problem I have included at the
> >bottom of this post the code of the 4 files. I show results with the
> >gnu compiler but i have the same results with the intel compiler.
> >
> >file1.cpp compiles without a problem, the problem occurs when compiling
> >file2.cpp. It complains about not finding the constructor of the class
> >base.
> >
> >[klaas@blackdwarf cpptest]$ g++ -c -Wall file2.cpp
> >file2.cpp: In constructor 'doublederived::doublederived(int)':
> >file2.cpp:3: error: no matching function for call to 'base::base()'
> >file1.hpp:3: note: candidates are: base::base(int)
> >file1.hpp:1: note: base::base(const base&)
> >
> >When derived does not virtual inherit base, this does not give a
> >problem. Is there a reason why this is not working?
> >
> >Regards
> >Klaas
> >
> >// file1.hpp
> >class base {
> > public:
> > base(int);
> >};
> >
> >class derived : public virtual base {
> > public:
> > derived(int);
> >};
> >
> >// file1.cpp
> >#include <iostream>
> >#include "file1.hpp"
> >base::base(int a) { std::cout << a << std::endl;}
> >derived::derived(int a) : base(a) { }
> >
> >// file2.hpp
> >#include "file1.hpp"
> >class doublederived : public derived {
> > public:
> > doublederived(int);
> >};
> >
> >// file2.cpp
> >#include "file2.hpp"
> >doublederived::doublederived(int a) : derived(a) { }

>
> I'm not real sharp on this one. I looked at Eckel's and found this (in
> diamond example):
>
> class Left : virtual public Top { public: <snip>
> Left(int m, int n) : Top(m) { y = n; }
> };
>
> class Right : virtual public Top { public: <snip>
> Right(int m, int n) : Top(m) { z = n; }
> };
>
> class Bottom : public Left, public Right {
> int w;
> public:
> Bottom(int i, int j, int k, int m)
> : Top(i), Left(0, j), Right(0, k) { w = m; }
> };
>
> Note how, even though Bottom did not directly derive from Top, it was still
> initialised in Bottom.
>
> So, maybe try(?):
>
> doublederived::doublederived(int a) : base(a), derived(a) { }
>
> Makes since to me because your error is in looking for a base default
> constructor, instead of using your defined constructor.
>
> I'm just guessing at this, but, it shouldn't hurt to try it.
> I tried it (in a single file) and it worked.
>
> --
> Bob R
> POVrookie


 
Reply With Quote
 
Klaas Vantournhout
Guest
Posts: n/a
 
      11-20-2006
Thanks Bob and Frank, indeed that was it. The most derived class must
explicitly invoke the virutal base class's constructor.

Great

thanks both

klaas


Frank wrote:
> I think BobR gave the correct solution. In this case, virtual
> inheritance makes the constructor of "base" delay to the most derived
> class "'doublederived" and suppresses the call of it from "derived".
> Actually, in the constructor of " base", if you don't invoke the
> constructor of "base", its default constructor will be the first
> candidate.
>
> "BobR 写道:
> "
>> Klaas Vantournhout wrote in message
>> <4560e845$0$1120$>...
>>> Hi,
>>>
>>> Recently I obtained a problem with virtual inheritance when implementing
>>> it in multiple files. To present the problem I have included at the
>>> bottom of this post the code of the 4 files. I show results with the
>>> gnu compiler but i have the same results with the intel compiler.
>>>
>>> file1.cpp compiles without a problem, the problem occurs when compiling
>>> file2.cpp. It complains about not finding the constructor of the class
>>> base.
>>>
>>> [klaas@blackdwarf cpptest]$ g++ -c -Wall file2.cpp
>>> file2.cpp: In constructor 'doublederived::doublederived(int)':
>>> file2.cpp:3: error: no matching function for call to 'base::base()'
>>> file1.hpp:3: note: candidates are: base::base(int)
>>> file1.hpp:1: note: base::base(const base&)
>>>
>>> When derived does not virtual inherit base, this does not give a
>>> problem. Is there a reason why this is not working?
>>>
>>> Regards
>>> Klaas
>>>
>>> // file1.hpp
>>> class base {
>>> public:
>>> base(int);
>>> };
>>>
>>> class derived : public virtual base {
>>> public:
>>> derived(int);
>>> };
>>>
>>> // file1.cpp
>>> #include <iostream>
>>> #include "file1.hpp"
>>> base::base(int a) { std::cout << a << std::endl;}
>>> derived::derived(int a) : base(a) { }
>>>
>>> // file2.hpp
>>> #include "file1.hpp"
>>> class doublederived : public derived {
>>> public:
>>> doublederived(int);
>>> };
>>>
>>> // file2.cpp
>>> #include "file2.hpp"
>>> doublederived::doublederived(int a) : derived(a) { }

>> I'm not real sharp on this one. I looked at Eckel's and found this (in
>> diamond example):
>>
>> class Left : virtual public Top { public: <snip>
>> Left(int m, int n) : Top(m) { y = n; }
>> };
>>
>> class Right : virtual public Top { public: <snip>
>> Right(int m, int n) : Top(m) { z = n; }
>> };
>>
>> class Bottom : public Left, public Right {
>> int w;
>> public:
>> Bottom(int i, int j, int k, int m)
>> : Top(i), Left(0, j), Right(0, k) { w = m; }
>> };
>>
>> Note how, even though Bottom did not directly derive from Top, it was still
>> initialised in Bottom.
>>
>> So, maybe try(?):
>>
>> doublederived::doublederived(int a) : base(a), derived(a) { }
>>
>> Makes since to me because your error is in looking for a base default
>> constructor, instead of using your defined constructor.
>>
>> I'm just guessing at this, but, it shouldn't hurt to try it.
>> I tried it (in a single file) and it worked.
>>
>> --
>> Bob R
>> POVrookie

>

 
Reply With Quote
 
 
 
Reply

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 On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
inheritance, multiple inheritance and the weaklist and instance dictionaries Rouslan Korneychuk Python 8 02-10-2011 04:02 AM
virtual inheritance and virtual function. Ashwin C++ 2 08-01-2006 12:48 PM
Text files read multiple files into single file, and then recreate the multiple files googlinggoogler@hotmail.com Python 4 02-13-2005 05:44 PM
mul. inheritance & overloading operator new/delete solved by virtual base inheritance? cppsks C++ 0 10-27-2004 07:49 PM
Multiple and virtual inheritance, and downcasting Stuart Golodetz C++ 6 08-30-2003 02:31 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57