Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

no default constructor exists for class x (inheritance) C++

Tags:

c++

I have the following three headers:

IBaseStates.h

class IBaseStates
{
public:
    enum STATE;

    virtual void Update( STATE state ) = 0;
};

PlayerStates.h

#pragma once

#include "IBaseStates.h"
#include "Player.h"

class PlayerStates : public IBaseStates, public Player
{
public:
    enum STATE {
        FLYING,
        FALLING
    };

    int textureAmount;

    PlayerStates();
    ~PlayerStates( );

    void Update( STATE state );
};

Player.h

#pragma once

#include "StructVertex.h"
#include "SquareVertices.h"
#include "WICTextureLoader.h"

using namespace Microsoft::WRL;
using namespace DirectX;
//using namespace DirectX;

class Player : public SquareVertices
{
public:
    Player( ComPtr<ID3D11Device1> d3dDevice );
    ~Player();

    void Initialize();
    void Update();

    float x;
    float y;
    float z;
    float rotation;
    float velocity;

    ComPtr<ID3D11Buffer> vertexbuffer;
    ComPtr<ID3D11Buffer> indexbuffer;
    ComPtr<ID3D11ShaderResourceView> texture[3];
protected:
    const ComPtr<ID3D11Device1> d3dDevice;
};

Why when I define a constructor for my PlayerStates class in my PlayerStates.cpp file do I get

no default constructor exists for class Player

PlayerStates.cpp

#include "pch.h"
#include "PlayerStates.h"

PlayerStates::PlayerStates()
: 
    textureAmount( 3 )
{
}
like image 415
Jimmyt1988 Avatar asked Jan 08 '14 21:01

Jimmyt1988


People also ask

What happens if there is no default constructor?

A default constructor is a constructor that either has no parameters, or if it has parameters, all the parameters have default values. If no user-defined constructor exists for a class A and one is needed, the compiler implicitly declares a default parameterless constructor A::A() .

Can a class have no default constructor?

No default constructor is created for a class that has any constant or reference type members. A constructor of a class A is trivial if all the following are true: It is implicitly defined. A has no virtual functions and no virtual base classes.

Do you need to have a default constructor C++?

For example, all members of class type, and their class-type members, must have a default constructor and destructors that are accessible. All data members of reference type and all const members must have a default member initializer.


1 Answers

When you declare a non-default constructor for a class, the compiler does not generate a default one anymore. So you have to provide your own.

PlayerStates needs to call a default constructor of its base class Player in its own default constructor. So you either need to provide Player with a default constructor, or call it's non-default constructor from PlayerStates' default constructor initialization list.

This implicitly calls Player::Player().

PlayerStates::PlayerStates() : textureAmount( 3 ) {}

This calls the single argument constructor:

PlayerStates::PlayerStates() : Player(someComPtr), textureAmount( 3 ) {}
like image 169
juanchopanza Avatar answered Nov 11 '22 03:11

juanchopanza