没有为类x(继承)C ++存在默认构造函数 [英] no default constructor exists for class x (inheritance) C++
问题描述
我有以下三个标题:
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;
};
为什么当我为PlayerStates.cpp文件中的PlayerStates类定义一个构造函数时, p>
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 )
{
}
推荐答案
当你声明一个类的非默认构造函数时,不再生成默认的。所以你必须提供自己的。
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
需要调用其基类的默认构造函数 Player
在其自己的默认构造函数。所以你需要用默认构造函数提供 Player
,或者从 PlayerStates
'默认构造函数中调用非默认构造函数初始化列表。
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.
这隐含地调用 Player :: Player()
。
PlayerStates::PlayerStates() : textureAmount( 3 ) {}
这将调用单个参数构造函数:
This calls the single argument constructor:
PlayerStates::PlayerStates() : Player(someComPtr), textureAmount( 3 ) {}
这篇关于没有为类x(继承)C ++存在默认构造函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!