Alex Tremblay
Alex Tremblay

Reputation: 11

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

Upvotes: 0

Views: 223

Answers (3)

Bernardo Reis
Bernardo Reis

Reputation: 49

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)

Upvotes: 0

Stephen Newell
Stephen Newell

Reputation: 7828

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} { }

Upvotes: 1

lmpall4
lmpall4

Reputation: 43

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(){}

Upvotes: 0

Related Questions