簡體   English   中英

'GameObject':沒有合適的默認構造函數

[英]'GameObject': no appropriate default constructor available

我正在嘗試使用GameObject創建游戲,其中包括瓷磚,玩家,敵人和牆壁。 我正在嘗試將我的角色Character(又兩個父類Player和Enemy父類)添加到基類GameObject。 嘗試創建Characters的構造函數時,我在Character.cpp文件中收到C2512錯誤。 誰能指出我可能做錯了什么? 提前致謝。

字符數

#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

游戲對象

#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);

字符.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的默認構造函數的調用。

如果您沒有為GameObject類實現任何構造函數,則編譯器將為其生成一個空的默認構造函數。 但是,您已經為GameObject類實現了一個構造函數,因此編譯器不會這樣做。 您應該自己為其提供默認構造函數。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM