Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.2k views
in Technique[技术] by (71.8m points)

no default constructor exists for class x (inheritance) 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 )
{
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...