3 level inheritance heirarchy



 DEVELOP > c-Plus-Plus > 3 level inheritance heirarchy

LINK TO THIS PAGE  


rating :  0   |  0


  Page 1 of 1

1

 
Topic: DEVELOP > c-Plus-Plus
User: ""
Date: 21 Dec 2007 03:05:39 AM
Object: 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.
.

User: "Sachin"

Title: Re: 3 level inheritance heirarchy 21 Dec 2007 04:34:41 AM
On Dec 21, 2:05=A0pm,
wrote:

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: =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 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
{
=A0public:
=A0 virtual ~ILev0() {}
=A0 virtual void PrintLev0() =3D 0;

};

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

};

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

};

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

=A0protected:
=A0 int m_nNum0;

};

class Lev1: public virtual Lev0, public virtual ILev1
{
=A0public:
=A0 Lev1(int nNum0, int nNum1);
=A0 virtual void PrintLev1();
=A0protected:
=A0 int m_nNum1;

};

class Lev2: public virtual Lev1, public virtual ILev2
{
=A0public:
=A0 Lev2(int nNum0, int nNum1, int nNum2);
=A0 virtual void PrintLev2();
=A0protected:
=A0 int m_nNum2;

};

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

//-------

Lev0::Lev0(int nNum)
=A0 :m_nNum0(nNum)
{

}

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

}

//-------

Lev1::Lev1(int nNum0, int nNum1)
=A0 :Lev0(nNum0),
=A0 =A0m_nNum1(nNum1)
{

}

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

}

//-------

Lev2::Lev2(int nNum0, int nNum1, int nNum2)
=A0 :Lev1(nNum0, nNum1),
=A0 =A0m_nNum2(nNum2)
{

}

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

}

//---

main()
{
=A0 Lev0 *b =3D new Lev0(10);
=A0 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)
{
}
.
User: ""

Title: Re: 3 level inheritance heirarchy 21 Dec 2007 10:44:17 AM
On Dec 21, 2:34 am, Sachin <sachinc.bira...@gmail.com> wrote:

On Dec 21, 2:05 pm,

wrote:



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)
{

}

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 ?
.
User: "Tadeusz B. Kopec"

Title: Re: 3 level inheritance heirarchy 21 Dec 2007 02:40:33 PM
On Fri, 21 Dec 2007 08:44:17 -0800, interec wrote:

On Dec 21, 2:34 am, Sachin <sachinc.bira...@gmail.com> wrote:

On Dec 21, 2:05 pm,

wrote:



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)
{

}


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
.
User: "Sachin"

Title: Re: 3 level inheritance heirarchy 23 Dec 2007 11:48:36 PM
On Dec 22, 1:40=A0am, "Tadeusz B. Kopec" <tko...@NOSPAMPLEASElife.pl>
wrote:

On Fri, 21 Dec 2007 08:44:17 -0800, interec wrote:

On Dec 21, 2:34 am, Sachin <sachinc.bira...@gmail.com> wrote:

On Dec 21, 2:05 pm,

wrote:


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: =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 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
{
=A0public:
=A0 virtual ~ILev0() {}
=A0 virtual void PrintLev0() =3D 0;


};


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


};


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


};


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


=A0protected:
=A0 int m_nNum0;


};


class Lev1: public virtual Lev0, public virtual ILev1 {
=A0public:
=A0 Lev1(int nNum0, int nNum1);
=A0 virtual void PrintLev1();
=A0protected:
=A0 int m_nNum1;


};


class Lev2: public virtual Lev1, public virtual ILev2 {
=A0public:
=A0 Lev2(int nNum0, int nNum1, int nNum2); virtual void PrintLev2();
=A0protected:
=A0 int m_nNum2;


};


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


//-------


Lev0::Lev0(int nNum)
=A0 :m_nNum0(nNum)
{


}


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


}


//-------


Lev1::Lev1(int nNum0, int nNum1)
=A0 :Lev0(nNum0),
=A0 =A0m_nNum1(nNum1)
{


}


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


}


//-------


Lev2::Lev2(int nNum0, int nNum1, int nNum2)
=A0 :Lev1(nNum0, nNum1),
=A0 =A0m_nNum2(nNum2)
{


}


void Lev2::PrintLev2()
{
=A0 std::cout << "Lev2 Num: " << m_nNum0 << ", " << m_nNum1 << ", " <=

<

m_nNum2 << std::endl;


}


//---


main()
{
=A0 Lev0 *b =3D new Lev0(10);
=A0 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)
=A0 :Lev1(nNum0, nNum1),Lev0(nNum0),
=A0 =A0m_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 (tko...@NOSPAMPLEASElife.pl)
Enzymes are things invented by biologists that explain things which
otherwise require harder thinking.
=A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 -- 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.
.





  Page 1 of 1

1

 


Related Articles
 

NEWER

pg.1232     pg.940     pg.716     pg.544     pg.412     pg.311     pg.234     pg.175     pg.130     pg.96     pg.70     pg.50     pg.35     pg.24     pg.16     pg.10     pg.6     pg.3     pg.1

OLDER