5

I am using this pretty simple class without using any inheritance.

class A
{
  int a;
  int b;

public:
 A(int x, int y) { a = x; b = y;}
 A() :A(0,0){};
 ~A(){};
} ;

int main ()
{
  A a1, a2(5, 7) ;
}

I get this error.

error C2614: 'A' : illegal member initialization: 'A' is not a base or member

There are similar questions on SO but they relate to inheritance. Can someone explain the reason and what does standard say about that?

EDIT:

It would be better if someone elaborate more on the forwarding constructor and this feature in C++11.

Coding Mash
  • 3,338
  • 5
  • 24
  • 45
  • This compiles fine for me but not in gcc – David G Oct 20 '12 at 11:45
  • looks like you're using Visual Studio 2010(or previous), it doesn't work in VS2010 even. I heard that more features of C++11 have been added to SP1, and VS2012 but I am not too sure. – Aniket Inge Oct 20 '12 at 11:48
  • See [n1986](http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2006/n1986.pdf). – Mankarse Oct 20 '12 at 11:52
  • Possible duplicate of [C++11 calling constructor from constructor of same class type](http://stackoverflow.com/questions/10423401/c11-calling-constructor-from-constructor-of-same-class-type) – Florian Castellane Jul 21 '16 at 12:14

5 Answers5

5

If you can use C++11, you could initialize A() from A(int, int). This is not possible in C++03, where you have to write two separate constructors.

If you want your code to work in C++03, you have two options:

  • Create a function init(int, int) and call it from each of your constructors. This is a good choice if your constructor does a lot of work.
  • Duplicate behaviour in both constructors. This is a good choice when all you are doing are member initializations.

You can also call a base constructor from a child class constructor. For instance, if you have

class A {
    A(int, int);
};
class B : public A {
    B(int, int);
};

You could write

B::B(int x, int y) : A(x,y) {}

This is what your compiler means when it says that A is not a base, it is expecting this situation.

All of these are compatible with C++03.

You could also upgrade your compiler to support C++11 features. I wouldn't recommend this if you are working in Linux and want your project to compile in Windows because Windows compilers don't implement all the C++ features that Linux compilers do (unless you pay for a good compiler).

alestanis
  • 21,519
  • 4
  • 48
  • 67
1

that's because of your problem with the constructor at :

From the looks of your error message, I am assuming you're on Visual Studio (probably 2010) and I agree, it doesn't work in VS2010.

A:A(0,0){ }

Fix for VS 2010 and its predecessors: A():a(0),b(0){}

Aniket Inge
  • 25,375
  • 5
  • 50
  • 78
0

You can not do this way A() :A(0,0){}; this way is used to initialize class base members.

A(){a=0;b=0;};

or if you wish define private: void set(int x, int y); and use it in constructors.

0

You are attempting to use a delegating constructor, which is a language feature that was only introduced in C++11, and which is not yet implemented in every compiler.

Mankarse
  • 39,818
  • 11
  • 97
  • 141
0

A() :A(0,0){}; is called constructor chaining.

You should check that you compiler accept C++11. Constructor chaining is only valid in C++11. It was not available before.

What compiler do you use ?

Stephane Rolland
  • 38,876
  • 35
  • 121
  • 169