Compiler Error: No appropriate default constructor avaible

I am doing OOP in C++ for the first time. I have noticed it's very different from other languages in which I have done OOP in the past.

It's been going great so far, but I have encountered an issue where I need a constructor to receive an object that I have created as parameter and for some reasons it refuses to compile and throws errors.

I made a deep research about the problem online, but I do not see cases that look enough like mine and answers vary a lot. I would like the proper way to solve this problem, so I can follow these conventions throughout my whole project.

Here is the header file where the error has been thrown (Player.h):

#pragma once

// Header files
#include "Square.h"

class Player
{
private:
    // Private variables
    Square _position;

public:
    // Public constructors declarations
    Player(Square position);

    // Public functions declaration
    void setPosition(Square position);
    Square getPosition();
};

Here is the CPP file where the error has been thrown (Player.cpp):

// Header files
#include "Player.h"

// Public constructors
Player::Player(Square position) // <---------- ERROR LOCATION
{
    _position = position;
}

// Public functions
void Player::setPosition(Square position)
{
    _position = position;
}

Square Player::getPosition()
{
    return _position;
}

Just in case, here is the header file of the parameter object (Square.h):

#pragma once

class Square
{
private:
    // Private variables
    int _x;
    int _y;

public:
    // Public constructors declarations
    Square(int x, int y);

    // Public functions declaration
    void setX(int x);
    int getX();
    void setY(int y);
    int getY();
};

Here is also the CPP file of the parameter object (Square.cpp):

// Header files
#include "Square.h"

// Public constructors
Square::Square(int x, int y)
{
    _x = x;
    _y = y;
}

// Public functions
void Square::setX(int x)
{
    _x = x;
}

int Square::getX()
{
    return _x;
}

void Square::setY(int y)
{
    _y = y;
}

int Square::getY()
{
    return _y;
}

Here are the errors thrown by the compiler:

At line 4 of the file 'Player.cpp':

Error E0291: no default constructor exists for class 'Square'

Error C2512: 'Square' : no appropriate default constructor avaible

The issue is that Player::_position needs to be constructed before your opening brace in any Player constructor. You can either

  1. Create a default constructor (one that can be called without arguments) for Square. This may or may not be appropriate for your program.
  2. Use an initializer list. This avoids design issues if Square can't have a default constructor for some reason.

The initializer list solution looks like this:

Player::Player(Square position)
 : _position{position} { }

Given that you implemented a constructor for Square, the compiler doesn't implement the default constructor. Your only way to construct Square is using the constructor you defined. Once you declared the Player's member variable Square _position, it shall be initialized somehow on Player's constructor. However, the compiler can't use the constructor you provided.

Looks like the default constructor is implicitly deleted. You may try adding a trivial one as follows (as public in Square.h.

Square(): _x(0), _y(0){}
~Square(){}

The C++ compiler produces an error of the form "'MyImplementationType_base<MyImplementationType>': no appropriate default constructor available". This can happen when you have derived from a type that has a non-trivial constructor. Your derived type's constructor needs to pass along the parameters that the base type's constructor needs.

Given that you implemented a constructor for Square, the compiler doesn't implement the default constructor. Your only way to construct Square is using the constructor you defined.

Once you declared the Player's member variable Square _position, it shall be initialized somehow on Player's constructor. However, the compiler can't use the constructor you provided.

You can declare the default constructor yourself, by:

Square() = default;

The error code is shown in the first line of the constructor, because the compiler is trying to initialize every member of Player before executing the body of the constructor, but it can't find a suitable way to construct Square.

Another solution is to initialize the member variable directly using the constructor you provided. This way, the compiler will use your constructor when initializing Player's member variables:

Square _position = Square(0,0)

This topic has been deleted. Only users with topic management privileges can see it.

I'm getting compile error: 'DList' : no appropriate default constructor available That's a poorly diagnosed message. What this means is that the template DList must have an argument list. Jonathan

The compiler tries to do this for you, but can't because Triangle has no default constructor and the compiler isn't allowed to create one because Triangle already has the one you defined. The solution is to write one yourself; if DFVertex has a suitable default constructor, an empty one should suffice:

If you require it, then you must provide the appropriate constructor. Marked as answer by Yan-Fei Wei Friday, June 13, 2008 2:02 AM Saturday, June 7, 2008 10:27 PM

Comments
  • Sidenote: Player::Player(Square position) will copy the Square. That's not much work since it's a pair of ints, but something to watch out for with larger objects. You may want Player::Player(const Square &position) to pass by reference to save on copying. It's tagged const because it widens what you can pass in and making everything const until proven otherwise helps prevent some types of bugs from creeping in.
  • You might want to watch this cppcon talk on OOP in modern C++. In particular using override to tag your methods can prevent a lot of grief when you accidentally overload. youtube.com/watch?v=32tDTD9UJCE