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

c++
16,197

Solution 1

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

Solution 2

Player can't be created with out a ComPtr<ID3D11Device1> d3dDevice, so you're PlayerStates constructor is going to have to find one and pass it to Player.

PlayerStates::PlayerStates()
: 
  Player( globalD3DDevice ),
  textureAmount( 3 )
{
}

Or, if that's not what you intended, turn the inheritance around like you mentioned in the comments.

Share:
16,197
Jimmyt1988
Author by

Jimmyt1988

Updated on June 11, 2022

Comments

  • Jimmyt1988
    Jimmyt1988 7 months

    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 )
    {
    }
    
  • Jimmyt1988
    Jimmyt1988 almost 9 years
    I have declared a cpp version of a Player constructor... is that what you mean?
  • tabstop
    tabstop almost 9 years
    You declared one that takes an argument, but that doesn't help in this case (since you don't have a d3dDevice in your PlayerStates constructor).
  • juanchopanza
    juanchopanza almost 9 years
    @JamesT no, you need to give Player a default constructor.
  • Raja
    Raja almost 9 years
    A default constructor is one without arguments.
  • Jimmyt1988
    Jimmyt1988 almost 9 years
    I know I deleted my comment before, but going off of the same basis as that... d3dDevice that I want to be kept inside Players' belly is no longer accessible by PlayerStates... even if I make PlayerStates a friend of Player... Maybe the other way round was correct?
  • woolstar
    woolstar almost 9 years
    Does PlayerStates need access to the ID3D11Device1 ?
  • Jimmyt1988
    Jimmyt1988 almost 9 years
    Yes indeedy. I want the PlayerStates to have access to the Device so that I can load textures... the texture loading function requires a pointer to the device... I'm doing this inheritance to organize the code better... what do you think?
  • Jimmyt1988
    Jimmyt1988 almost 9 years
    You know what, don't worry.. I'll just sort something else out, thanks for your help dude