'GameObject': 没有合适的默认构造函数可用

'GameObject': no appropriate default constructor available

我正在尝试创建一个游戏,其中 GameObject 是方块、玩家、敌人和墙壁等所有内容。我试图让我的 class 角色(Parent 到另外两个 classes 玩家和敌人)到基础 class 游戏对象。当我尝试为角色创建构造函数时,我在 Character.cpp 文件中收到 C2512 错误。谁能指出我可能做错了什么?提前致谢。

Characters.h

#ifndef CHARACTERS_H
#define CHARACTERS_H

#include <cstdlib>    
#include <ctime>      /*cstdlib and ctime are used to help with the pseudo randomness for events*/
#include <cstdio>   /*allows remove function*/
#include <iostream>
#include <fstream>
#include <sstream>
#include <array>
#include <string>
#include <vector>
#include "GameObject.h"

using namespace std;

class Character : public GameObject{
public:
    Character();
    ~Character();
    virtual void attack(vector<Character *>&characters) = 0;
    void set_values(string nam, int hp, int str, int mag) {
        name = nam;
        health = hp;
        strength = str;
        magic = mag;
    };
    string name;
    int health;
    int strength;
    int magic;

};


#endif

GameObject.h

#ifndef GAMEOBJECCT_H
#define GAMEOBJECT_H

#include "Direct3D.h"
#include "Mesh.h"
#include "InputController.h"

class GameObject {
protected:
    Vector3 m_position;
    float m_rotX, m_rotY, m_rotZ;
    float m_scaleX, m_scaleY, m_scaleZ; //Not really needed for A1.
    //Used for rendering
    Matrix m_world;
    Mesh* m_mesh;
    Texture* m_texture;
    Shader* m_shader;
    InputController* m_input; //Might have enemies who react to player input.

    float m_moveSpeed;
    float m_rotateSpeed;

public:
    //Constructor
    GameObject(Mesh* mesh, Shader* shader, InputController *input, Vector3 position, Texture* texture);
    ~GameObject();
    void Update(float timestep);
    void Render(Direct3D* renderer, Camera* cam);

Characters.cpp

#include "Characters.h"
#include <cstdlib>    
#include <ctime>      /*cstdlib and ctime are used to help with the pseudo randomness for events*/
#include <cstdio>   /*allows remove function*/
#include <iostream>
#include <fstream>
#include <sstream>
#include <array>
#include <string>
#include <vector>
using namespace std;

void Character::attack(vector<Character *>&characters) {};

Character::Character() {

};
Character::~Character() {};

您的 GameObject 应该有一个默认构造函数(没有参数的构造函数)。

因为在 Character::Character() 中,编译器将生成对 GameObject 的默认构造函数的调用。

如果您不为 class GameObject 实现任何构造函数,编译器将为它生成一个空的默认构造函数。但是你已经实现了 class GameObject 的构造函数,所以编译器不会这样做。你应该自己为它提供一个默认的构造函数。

Character::Character() {};

相当于

Character::Character() : GameObject() {};

由于GameObject没有默认构造函数,编译器会产生错误。除非 GameObject 的用户定义构造函数使用的所有参数都有合理的默认值,否则您应该更改 Character 以拥有一个用户定义的构造函数,该构造函数接受构造 [=] 所需的所有参数14=].

Character(Mesh* mesh,
          Shader* shader,
          InputController *input,
          Vector3 position,
          Texture* texture);

并将其实现为:

Character::Character(Mesh* mesh,
                     Shader* shader,
                     InputController *input,
                     Vector3 position,
                     Texture* texture) : GameObject(mesh, shader, input, position, texture) {}