2

I am trying to make an explicit constructor that calls the default constructor, but it says that i don't have one.

class Paddle{
private:

    int x, y;
    int startX, startY;

public:

    Paddle(){
        x = y = 0;
    }

    Paddle(int posX, int posY) : Paddle(){  // <-- the error is on ": Paddle()"
        startX = posX;
        startY = posY;
        x = posX;
        y = posY;
    }
};

What exactly causes this to happen, and how can i fix it? Thanks in advance!

Georgi Enev
  • 23
  • 1
  • 9

4 Answers4

7

This is the correct syntax, but constructor delegation is not supported until C++11.

Visual Studio 2012 does not purport to implement the C++11 standard. Constructor delegation is one of those things that is doesn't support.

2

You mentioned you use visual studio 2012. Unfortunately it doesn't support delegating constructors. You can see a table of supported features by version at msdn.

https://msdn.microsoft.com/en-us/library/hh567368.aspx

StoryTeller - Unslander Monica
  • 165,132
  • 21
  • 377
  • 458
0

C++ don't allow a constructor to call other constructors definition of the same class at the initialization level.

When you write Paddle(int posX, int posY) : Paddle() ... the compiler understands Paddle is a super class of Paddle or a member of Paddle which is not the case. All what you need is a default parameters like this:

class Paddle{
private:

    int x, y;
    int startX, startY;

public:
    Paddle(int posX=0, int posY=0):x(posX),y(posY),startX(posX),startY(posY) {}
};
k-messaoudi
  • 347
  • 2
  • 5
-1

this is allowed in C++11 see Object construction

k-messaoudi
  • 347
  • 2
  • 5