Connecting Tech Pros Worldwide Help | Site Map

3 level inheritance heirarchy

 
LinkBack Thread Tools Search this Thread
  #1  
Old December 21st, 2007, 08:15 AM
interec@interec.net
Guest
 
Posts: n/a
Default 3 level inheritance heirarchy

I have trying to set up a three level inheritance heirarchy (see files
test.h/cpp below). When I compile this code using g++, I get the
following errors:

bash-3.00$ g++ test.cpp
test.cpp: In constructor `Lev2::Lev2(int, int, int)':
test.cpp:33: error: no matching function for call to `Lev0::Lev0(const
void**)'
test.h:27: note: candidates are: Lev0::Lev0(const Lev0&)
test.cpp:6: note: Lev0::Lev0(int)

Can someone please explain whats going on and how I can fix this
problem. Please see code below. If I remove the classes ILev2 and Lev2
everything compiles fine. Thanks

//-------------------------- FILE: test.h --------------------------//

#include <iostream>
#include <iomanip>

class ILev0
{
public:
virtual ~ILev0() {}
virtual void PrintLev0() = 0;
};

class ILev1: public virtual ILev0
{
public:
virtual ~ILev1() {}
virtual void PrintLev1() = 0;
};

class ILev2: public virtual ILev1
{
public:
virtual ~ILev2() {}
virtual void PrintLev2() = 0;
};


class Lev0: public virtual ILev0
{
public:
Lev0(int nNum);
virtual void PrintLev0();

protected:
int m_nNum0;
};

class Lev1: public virtual Lev0, public virtual ILev1
{
public:
Lev1(int nNum0, int nNum1);
virtual void PrintLev1();
protected:
int m_nNum1;
};


class Lev2: public virtual Lev1, public virtual ILev2
{
public:
Lev2(int nNum0, int nNum1, int nNum2);
virtual void PrintLev2();
protected:
int m_nNum2;
};



//-------------------------- FILE: test.cpp ------------------------//
#include "test.h"

//-------

Lev0::Lev0(int nNum)
:m_nNum0(nNum)
{
}

void Lev0::PrintLev0()
{
std::cout << "Lev0 Num: " << m_nNum0 << std::endl;
}

//-------

Lev1::Lev1(int nNum0, int nNum1)
:Lev0(nNum0),
m_nNum1(nNum1)
{
}

void Lev1::PrintLev1()
{
std::cout << "Lev1 Num: " << m_nNum0 << ", " << m_nNum1 <<
std::endl;
}

//-------

Lev2::Lev2(int nNum0, int nNum1, int nNum2)
:Lev1(nNum0, nNum1),
m_nNum2(nNum2)
{
}

void Lev2::PrintLev2()
{
std::cout << "Lev2 Num: " << m_nNum0 << ", " << m_nNum1 << ", " <<
m_nNum2 << std::endl;
}


//---

main()
{
Lev0 *b = new Lev0(10);
b->PrintLev0();
}


//------------------------------------------------------------------//

Never miss a thing. Make Yahoo your homepage.

  #2  
Old December 21st, 2007, 09:35 AM
Sachin
Guest
 
Posts: n/a
Default Re: 3 level inheritance heirarchy

On Dec 21, 2:05*pm, inte...@interec.net wrote:
Quote:
I have trying to set up a three level inheritance heirarchy (see files
test.h/cpp below). When I compile this code using g++, I get the
following errors:
>
bash-3.00$ g++ test.cpp
test.cpp: In constructor `Lev2::Lev2(int, int, int)':
test.cpp:33: error: no matching function for call to `Lev0::Lev0(const
void**)'
test.h:27: note: candidates are: Lev0::Lev0(const Lev0&)
test.cpp:6: note: * * * * * * * * Lev0::Lev0(int)
>
Can someone please explain whats going on and how I can fix this
problem. Please see code below. If I remove the classes ILev2 and Lev2
everything compiles fine. Thanks
>
//-------------------------- FILE: test.h --------------------------//
>
#include <iostream>
#include <iomanip>
>
class ILev0
{
*public:
* virtual ~ILev0() {}
* virtual void PrintLev0() = 0;
>
};
>
class ILev1: public virtual ILev0
{
*public:
* virtual ~ILev1() {}
* virtual void PrintLev1() = 0;
>
};
>
class ILev2: public virtual ILev1
{
*public:
* virtual ~ILev2() {}
* virtual void PrintLev2() = 0;
>
};
>
class Lev0: public virtual ILev0
{
*public:
* Lev0(int nNum);
* virtual void PrintLev0();
>
*protected:
* int m_nNum0;
>
};
>
class Lev1: public virtual Lev0, public virtual ILev1
{
*public:
* Lev1(int nNum0, int nNum1);
* virtual void PrintLev1();
*protected:
* int m_nNum1;
>
};
>
class Lev2: public virtual Lev1, public virtual ILev2
{
*public:
* Lev2(int nNum0, int nNum1, int nNum2);
* virtual void PrintLev2();
*protected:
* int m_nNum2;
>
};
>
//-------------------------- FILE: test.cpp ------------------------//
#include "test.h"
>
//-------
>
Lev0::Lev0(int nNum)
* :m_nNum0(nNum)
{
>
}
>
void Lev0::PrintLev0()
{
* std::cout << "Lev0 Num: " << m_nNum0 << std::endl;
>
}
>
//-------
>
Lev1::Lev1(int nNum0, int nNum1)
* :Lev0(nNum0),
* *m_nNum1(nNum1)
{
>
}
>
void Lev1::PrintLev1()
{
* std::cout << "Lev1 Num: " << m_nNum0 << ", " << m_nNum1 <<
std::endl;
>
}
>
//-------
>
Lev2::Lev2(int nNum0, int nNum1, int nNum2)
* :Lev1(nNum0, nNum1),
* *m_nNum2(nNum2)
{
>
}
>
void Lev2::PrintLev2()
{
* std::cout << "Lev2 Num: " << m_nNum0 << ", " << m_nNum1 << ", " <<
m_nNum2 << std::endl;
>
}
>
//---
>
main()
{
* Lev0 *b = new Lev0(10);
* b->PrintLev0();
>
}
>
//------------------------------------------------------------------//
>
Never miss a thing. Make Yahoo your homepage.
Call to Constructor of Lev0 class is missing in the Lev2 constructor.
Resolution is to either implement default constructor in Lev0 class or
make a call to Lev0 constructor in the constructor of Lev2 as follows:
Lev2::Lev2(int nNum0, int nNum1, int nNum2)
:Lev1(nNum0, nNum1),Lev0(nNum0),
m_nNum2(nNum2)
{

}
  #3  
Old December 21st, 2007, 03:45 PM
interec@interec.net
Guest
 
Posts: n/a
Default Re: 3 level inheritance heirarchy

On Dec 21, 2:34 am, Sachin <sachinc.bira...@gmail.comwrote:
Quote:
On Dec 21, 2:05 pm, inte...@interec.net wrote:
>
>
>
Quote:
I have trying to set up a three level inheritance heirarchy (see files
test.h/cpp below). When I compile this code using g++, I get the
following errors:
>
Quote:
bash-3.00$ g++ test.cpp
test.cpp: In constructor `Lev2::Lev2(int, int, int)':
test.cpp:33: error: no matching function for call to `Lev0::Lev0(const
void**)'
test.h:27: note: candidates are: Lev0::Lev0(const Lev0&)
test.cpp:6: note: Lev0::Lev0(int)
>
Quote:
Can someone please explain whats going on and how I can fix this
problem. Please see code below. If I remove the classes ILev2 and Lev2
everything compiles fine. Thanks
>
Quote:
//-------------------------- FILE: test.h --------------------------//
>
Quote:
#include <iostream>
#include <iomanip>
>
Quote:
class ILev0
{
public:
virtual ~ILev0() {}
virtual void PrintLev0() = 0;
>
Quote:
};
>
Quote:
class ILev1: public virtual ILev0
{
public:
virtual ~ILev1() {}
virtual void PrintLev1() = 0;
>
Quote:
};
>
Quote:
class ILev2: public virtual ILev1
{
public:
virtual ~ILev2() {}
virtual void PrintLev2() = 0;
>
Quote:
};
>
Quote:
class Lev0: public virtual ILev0
{
public:
Lev0(int nNum);
virtual void PrintLev0();
>
Quote:
protected:
int m_nNum0;
>
Quote:
};
>
Quote:
class Lev1: public virtual Lev0, public virtual ILev1
{
public:
Lev1(int nNum0, int nNum1);
virtual void PrintLev1();
protected:
int m_nNum1;
>
Quote:
};
>
Quote:
class Lev2: public virtual Lev1, public virtual ILev2
{
public:
Lev2(int nNum0, int nNum1, int nNum2);
virtual void PrintLev2();
protected:
int m_nNum2;
>
Quote:
};
>
Quote:
//-------------------------- FILE: test.cpp ------------------------//
#include "test.h"
>
Quote:
//-------
>
Quote:
Lev0::Lev0(int nNum)
:m_nNum0(nNum)
{
>
Quote:
}
>
Quote:
void Lev0::PrintLev0()
{
std::cout << "Lev0 Num: " << m_nNum0 << std::endl;
>
Quote:
}
>
Quote:
//-------
>
Quote:
Lev1::Lev1(int nNum0, int nNum1)
:Lev0(nNum0),
m_nNum1(nNum1)
{
>
Quote:
}
>
Quote:
void Lev1::PrintLev1()
{
std::cout << "Lev1 Num: " << m_nNum0 << ", " << m_nNum1 <<
std::endl;
>
Quote:
}
>
Quote:
//-------
>
Quote:
Lev2::Lev2(int nNum0, int nNum1, int nNum2)
:Lev1(nNum0, nNum1),
m_nNum2(nNum2)
{
>
Quote:
}
>
Quote:
void Lev2::PrintLev2()
{
std::cout << "Lev2 Num: " << m_nNum0 << ", " << m_nNum1 << ", " <<
m_nNum2 << std::endl;
>
Quote:
}
>
Quote:
//---
>
Quote:
main()
{
Lev0 *b = new Lev0(10);
b->PrintLev0();
>
Quote:
}
>
Quote:
//------------------------------------------------------------------//
>
Quote:
Never miss a thing. Make Yahoo your homepage.
>
Call to Constructor of Lev0 class is missing in the Lev2 constructor.
Resolution is to either implement default constructor in Lev0 class or
make a call to Lev0 constructor in the constructor of Lev2 as follows:
Lev2::Lev2(int nNum0, int nNum1, int nNum2)
:Lev1(nNum0, nNum1),Lev0(nNum0),
m_nNum2(nNum2)
{
>
}
Lev2 constructor explicitly calls Lev1 constructor and Lev1
constructor explicitly calls Lev0 constructor so why do I need to add
a call to Lev0 constructor in Lev2 ?
  #4  
Old December 21st, 2007, 07:45 PM
Tadeusz B. Kopec
Guest
 
Posts: n/a
Default Re: 3 level inheritance heirarchy

On Fri, 21 Dec 2007 08:44:17 -0800, interec wrote:
Quote:
On Dec 21, 2:34 am, Sachin <sachinc.bira...@gmail.comwrote:
Quote:
>On Dec 21, 2:05 pm, inte...@interec.net wrote:
>>
>>
>>
Quote:
I have trying to set up a three level inheritance heirarchy (see
files test.h/cpp below). When I compile this code using g++, I get
the following errors:
>>
Quote:
bash-3.00$ g++ test.cpp
test.cpp: In constructor `Lev2::Lev2(int, int, int)': test.cpp:33:
error: no matching function for call to `Lev0::Lev0(const void**)'
test.h:27: note: candidates are: Lev0::Lev0(const Lev0&) test.cpp:6:
note: Lev0::Lev0(int)
>>
Quote:
Can someone please explain whats going on and how I can fix this
problem. Please see code below. If I remove the classes ILev2 and
Lev2 everything compiles fine. Thanks
>>
Quote:
//-------------------------- FILE: test.h
--------------------------//
>>
Quote:
#include <iostream>
#include <iomanip>
>>
Quote:
class ILev0
{
public:
virtual ~ILev0() {}
virtual void PrintLev0() = 0;
>>
Quote:
};
>>
Quote:
class ILev1: public virtual ILev0
{
public:
virtual ~ILev1() {}
virtual void PrintLev1() = 0;
>>
Quote:
};
>>
Quote:
class ILev2: public virtual ILev1
{
public:
virtual ~ILev2() {}
virtual void PrintLev2() = 0;
>>
Quote:
};
>>
Quote:
class Lev0: public virtual ILev0
{
public:
Lev0(int nNum);
virtual void PrintLev0();
>>
Quote:
protected:
int m_nNum0;
>>
Quote:
};
>>
Quote:
class Lev1: public virtual Lev0, public virtual ILev1 {
public:
Lev1(int nNum0, int nNum1);
virtual void PrintLev1();
protected:
int m_nNum1;
>>
Quote:
};
>>
Quote:
class Lev2: public virtual Lev1, public virtual ILev2 {
public:
Lev2(int nNum0, int nNum1, int nNum2); virtual void PrintLev2();
protected:
int m_nNum2;
>>
Quote:
};
>>
Quote:
//-------------------------- FILE: test.cpp
------------------------// #include "test.h"
>>
Quote:
//-------
>>
Quote:
Lev0::Lev0(int nNum)
:m_nNum0(nNum)
{
>>
Quote:
}
>>
Quote:
void Lev0::PrintLev0()
{
std::cout << "Lev0 Num: " << m_nNum0 << std::endl;
>>
Quote:
}
>>
Quote:
//-------
>>
Quote:
Lev1::Lev1(int nNum0, int nNum1)
:Lev0(nNum0),
m_nNum1(nNum1)
{
>>
Quote:
}
>>
Quote:
void Lev1::PrintLev1()
{
std::cout << "Lev1 Num: " << m_nNum0 << ", " << m_nNum1 <<
std::endl;
>>
Quote:
}
>>
Quote:
//-------
>>
Quote:
Lev2::Lev2(int nNum0, int nNum1, int nNum2)
:Lev1(nNum0, nNum1),
m_nNum2(nNum2)
{
>>
Quote:
}
>>
Quote:
void Lev2::PrintLev2()
{
std::cout << "Lev2 Num: " << m_nNum0 << ", " << m_nNum1 << ", " <<
m_nNum2 << std::endl;
>>
Quote:
}
>>
Quote:
//---
>>
Quote:
main()
{
Lev0 *b = new Lev0(10);
b->PrintLev0();
>>
Quote:
}
>>
>>
//------------------------------------------------------------------//
Quote:
>>
Quote:
Never miss a thing. Make Yahoo your homepage.
>>
>Call to Constructor of Lev0 class is missing in the Lev2 constructor.
>Resolution is to either implement default constructor in Lev0 class or
>make a call to Lev0 constructor in the constructor of Lev2 as follows:
>Lev2::Lev2(int nNum0, int nNum1, int nNum2)
> :Lev1(nNum0, nNum1),Lev0(nNum0),
> m_nNum2(nNum2)
>{
>>
>}
>
Lev2 constructor explicitly calls Lev1 constructor and Lev1 constructor
explicitly calls Lev0 constructor so why do I need to add a call to Lev0
constructor in Lev2 ?
Because it's a virtual base class.

--
Tadeusz B. Kopec (tkopec@NOSPAMPLEASElife.pl)
Enzymes are things invented by biologists that explain things which
otherwise require harder thinking.
-- Jerome Lettvin
  #5  
Old December 24th, 2007, 04:55 AM
Sachin
Guest
 
Posts: n/a
Default Re: 3 level inheritance heirarchy

On Dec 22, 1:40*am, "Tadeusz B. Kopec" <tko...@NOSPAMPLEASElife.pl>
wrote:
Quote:
On Fri, 21 Dec 2007 08:44:17 -0800, interec wrote:
Quote:
On Dec 21, 2:34 am, Sachin <sachinc.bira...@gmail.comwrote:
Quote:
On Dec 21, 2:05 pm, inte...@interec.net wrote:
>
Quote:
Quote:
I have trying to set up a three level inheritance heirarchy (see
files test.h/cpp below). When I compile this code using g++, I get
the following errors:
>
Quote:
Quote:
bash-3.00$ g++ test.cpp
test.cpp: In constructor `Lev2::Lev2(int, int, int)': test.cpp:33:
error: no matching function for call to `Lev0::Lev0(const void**)'
test.h:27: note: candidates are: Lev0::Lev0(const Lev0&) test.cpp:6:
note: * * * * * * * * Lev0::Lev0(int)
>
Quote:
Quote:
Can someone please explain whats going on and how I can fix this
problem. Please see code below. If I remove the classes ILev2 and
Lev2 everything compiles fine. Thanks
>
Quote:
Quote:
//-------------------------- FILE: test.h
--------------------------//
>
Quote:
Quote:
#include <iostream>
#include <iomanip>
>
Quote:
Quote:
class ILev0
{
*public:
* virtual ~ILev0() {}
* virtual void PrintLev0() = 0;
>
Quote:
Quote:
};
>
Quote:
Quote:
class ILev1: public virtual ILev0
{
*public:
* virtual ~ILev1() {}
* virtual void PrintLev1() = 0;
>
Quote:
Quote:
};
>
Quote:
Quote:
class ILev2: public virtual ILev1
{
*public:
* virtual ~ILev2() {}
* virtual void PrintLev2() = 0;
>
Quote:
Quote:
};
>
Quote:
Quote:
class Lev0: public virtual ILev0
{
*public:
* Lev0(int nNum);
* virtual void PrintLev0();
>
Quote:
Quote:
*protected:
* int m_nNum0;
>
Quote:
Quote:
};
>
Quote:
Quote:
class Lev1: public virtual Lev0, public virtual ILev1 {
*public:
* Lev1(int nNum0, int nNum1);
* virtual void PrintLev1();
*protected:
* int m_nNum1;
>
Quote:
Quote:
};
>
Quote:
Quote:
class Lev2: public virtual Lev1, public virtual ILev2 {
*public:
* Lev2(int nNum0, int nNum1, int nNum2); virtual void PrintLev2();
*protected:
* int m_nNum2;
>
Quote:
Quote:
};
>
Quote:
Quote:
//-------------------------- FILE: test.cpp
------------------------// #include "test.h"
>
Quote:
Quote:
//-------
>
Quote:
Quote:
Lev0::Lev0(int nNum)
* :m_nNum0(nNum)
{
>
Quote:
Quote:
}
>
Quote:
Quote:
void Lev0::PrintLev0()
{
* std::cout << "Lev0 Num: " << m_nNum0 << std::endl;
>
Quote:
Quote:
}
>
Quote:
Quote:
//-------
>
Quote:
Quote:
Lev1::Lev1(int nNum0, int nNum1)
* :Lev0(nNum0),
* *m_nNum1(nNum1)
{
>
Quote:
Quote:
}
>
Quote:
Quote:
void Lev1::PrintLev1()
{
* std::cout << "Lev1 Num: " << m_nNum0 << ", " << m_nNum1 <<
std::endl;
>
Quote:
Quote:
}
>
Quote:
Quote:
//-------
>
Quote:
Quote:
Lev2::Lev2(int nNum0, int nNum1, int nNum2)
* :Lev1(nNum0, nNum1),
* *m_nNum2(nNum2)
{
>
Quote:
Quote:
}
>
Quote:
Quote:
void Lev2::PrintLev2()
{
* std::cout << "Lev2 Num: " << m_nNum0 << ", " << m_nNum1 << ", " <<
m_nNum2 << std::endl;
>
Quote:
Quote:
}
>
Quote:
Quote:
//---
>
Quote:
Quote:
main()
{
* Lev0 *b = new Lev0(10);
* b->PrintLev0();
>
Quote:
Quote:
}
>
Quote:
//------------------------------------------------------------------//
>
Quote:
Quote:
Never miss a thing. Make Yahoo your homepage.
>
Quote:
Quote:
Call to Constructor of Lev0 class is missing in the Lev2 constructor.
Resolution is to either implement default constructor in Lev0 class or
make a call to Lev0 constructor in the constructor of Lev2 as follows:
Lev2::Lev2(int nNum0, int nNum1, int nNum2)
* :Lev1(nNum0, nNum1),Lev0(nNum0),
* *m_nNum2(nNum2)
{
>
Quote:
Quote:
}
>
Quote:
Lev2 constructor explicitly calls Lev1 constructor and Lev1 constructor
explicitly calls Lev0 constructor so why do I need to add a call to Lev0
constructor in Lev2 ?
>
Because it's a virtual base class.
>
--
Tadeusz B. Kopec (tko...@NOSPAMPLEASElife.pl)
Enzymes are things invented by biologists that explain things which
otherwise require harder thinking.
* * * * * * * * -- Jerome Lettvin- Hide quoted text -
>
- Show quoted text -
I think you need to call Lev0 construcor from Lev2 constructor t0o, as
Lev0 constructor calls pror to Lev1 constructor.
 

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search

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

Popular Articles

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 220,989 network members.