【问题标题】:'GameObject': no appropriate default constructor available'GameObject':没有合适的默认构造函数可用
【发布时间】:2017-08-15 10:36:40
【问题描述】:

我正在尝试创建一个游戏,其中 GameObject 包含瓷砖、玩家、敌人和墙壁的所有内容。我正在尝试将我的类 Character(另外两个类 Player 和 Enemy 的父类)添加到基类 GameObject。当我尝试为 Characters 创建构造函数时,在 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() {};

【问题讨论】:

    标签: c++ inheritance parent-child default-constructor


    【解决方案1】:

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

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

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

    【讨论】:

    • 看起来不像向GameObject 添加默认构造函数是这里的意图或安全行为。我认为 R Sahu 的意图很重要..
    • 感谢您的帮助。我现在有另一个更简单的错误,即未解析的外部符号。这通常是您没有包含正确的内容?
    • @Zone 您是否实现了新添加的默认构造函数?因为如果你不这样做,肯定会导致未解决的符号错误。
    • 哦,是的,这似乎解决了问题。谢谢你,马丁。
    • @Zone 如果你对带有一堆nullptrs 的默认构造函数没问题,为什么还要实现GameObject?还是稍后再设置?
    【解决方案2】:
    Character::Character() {};
    

    等价于

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

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

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

    【讨论】:

      猜你喜欢
      • 2016-04-22
      • 1970-01-01
      • 1970-01-01
      • 2011-12-22
      • 2016-01-22
      • 2014-06-23
      • 1970-01-01
      • 2022-11-01
      • 2013-03-20
      相关资源
      最近更新 更多