gpt4 book ai didi

c++ - 类 x(继承)C++ 不存在默认构造函数

转载 作者:太空狗 更新时间:2023-10-29 23:30:24 25 4
gpt4 key购买 nike

我有以下三个标题:

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 类定义一个构造函数时,我会得到

no default constructor exists for class Player

PlayerStates.cpp

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

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

最佳答案

当您为类声明非默认构造函数时,编译器不再生成默认构造函数。所以你必须提供你自己的。

PlayerStates 需要在其自己的默认构造函数中调用其基类 Player 的默认构造函数。因此,您要么需要为 Player 提供默认构造函数,要么从 PlayerStates 的默认构造函数初始化列表中调用它的非默认构造函数。

这会隐式调用 Player::Player()

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

这会调用单参数构造函数:

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

关于c++ - 类 x(继承)C++ 不存在默认构造函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21006979/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com