2

I am dusting off my C++ learnings and trying to write a program here.

class Quad{
public:
Quad(){}
protected:
vec _topLeft, _topRight, _bottomLeft, _bottomRight;
};

class IrregularQuad : public Quad{
public:
IrregularQuad(vec topLeft, vec topRight, vec bottomLeft, vec bottomRight)
: _topLeft(topLeft), _topRight(topRight), _bottomLeft(bottomLeft), _bottomRight(bottomRight)
{}
};

I am getting a compile error on the above Dervied class contractor saying: Member initializer _topLeft does not name a non-static data memeber or base class (similar error for other members as well)

I can't get my head around what's going worng. Is it that I can't initialise protected members using Initalizer list or something?

Mikeb
  • 6,271
  • 3
  • 27
  • 41
user1240679
  • 6,829
  • 17
  • 60
  • 89
  • `vec` is another class I have defined for vectors(x, y, z) – user1240679 Jun 19 '13 at 16:33
  • 1
    possible duplicate of [Problem accessing base member in derived constructor](http://stackoverflow.com/questions/2947583/problem-accessing-base-member-in-derived-constructor) – Michael Kristofik Jun 19 '13 at 16:34
  • possible duplicate of [error C2614: 'ChildClass' : illegal member initialization: 'var1' is not a base or member](http://stackoverflow.com/questions/10138424/error-c2614-childclass-illegal-member-initialization-var1-is-not-a-base) – cHao Jun 20 '13 at 06:22

3 Answers3

3

Is it that I can't initialise protected members using Initalizer list or something?

Right. Only class' own members can be initialized in constructor initializer list (You can, OTOH, assign to them in constructor's body). The base subobjects are initialized first.

You'll need to somehow delegate the work to one of base class' constructors:

class Base {

    explicit Base(int i) : m(i)
    {}
protected:
    int m;
};

class Derived : public Base {
    explicit Derived(int i) : Base(i)
    { }
};
jrok
  • 54,456
  • 9
  • 109
  • 141
  • It stops implicit casts from ints. Otherwise you can write Base b = 42; See http://stackoverflow.com/questions/121162/what-does-the-explicit-keyword-in-c-mean/121163#121163 – doctorlove Jun 19 '13 at 16:37
  • It prevents implicit conversion from int to `Base`, like this: `Base b = 1; // will not work` But this does: `Base b = Base(1); // works, it's explicit` It's good practise to declare one argument constructors as explicit for this reason. – jrok Jun 19 '13 at 16:39
  • Of course, for every good practice, there's a reason it's not law. :) If your type is meant to represent a number, for example, `Base b = 1;` would make perfect sense, so implicit casting could be a good thing there. See the result with `std::string s = "stuff";` -- the class integrates with the language well enough that you almost forget that `"stuff"` isn't actually a `std::string`. – cHao Jun 19 '13 at 17:07
1

You don't initialize base class members in a derived class' initializer list. You can add a constructor to Quad to do that for you, or you can set them yourself in the body of the derived class constructor.

Michael Kristofik
  • 34,290
  • 15
  • 75
  • 125
  • Since the members are protected, I was thinking it could be done that way (using initializer list in derived class). – user1240679 Jun 19 '13 at 16:35
0

This is a duplicate of error C2614: 'ChildClass' : illegal member initialization: 'var1' is not a base or member

You can only initialise members or base classes in the initialiser list.

Community
  • 1
  • 1
doctorlove
  • 18,872
  • 2
  • 46
  • 62