Inheritance/Constructor problem
Author |
Message |
Dwindle
|
Posted: Mon Mar 27, 2006 10:50 am Post subject: Inheritance/Constructor problem |
|
|
code: | class Base
{
protected:
int x, y;
public:
Base() : x(0), y(0)
{ }
Base(int nx, int ny) : x(nx), y(ny)
{ }
};
class Derived : public Base
{
protected:
int z;
public:
Derived() : x(0), y(0), z(0)
{ }
}; |
I get an error at the Derived constructor line when I try to compile. Apparently the x and y fields don't exist but I know it does since it's inherited from the base class. Also if I put it as normal assignment statements inside the brackets it'll work but I really shouldn't have to do that should I? This is the simplified version of an actual program I'm working on and I wanted to split up the initializing variables part of the contructor from some stuff I actually wanted it to do. I'm using Dev-Cpp and I'm wondering if this code works ok for other people.
Also how do I indent certain lines when I post? I tried just adding spaces before but that doesnt work. |
|
|
|
|
|
Sponsor Sponsor
|
|
|
wtd
|
Posted: Mon Mar 27, 2006 12:47 pm Post subject: (No subject) |
|
|
Use code tags.
code: | [code]foo bar[/code] |
As for your problem... why are you not simply calling the Base class' constructor from the Derived class? |
|
|
|
|
|
Dwindle
|
Posted: Mon Mar 27, 2006 8:15 pm Post subject: (No subject) |
|
|
thanks, both problems solved. |
|
|
|
|
|
|
|