2017-03-23 15 views
0

GameObjectをタイル、プレーヤー、敵、壁のすべてから作成しようとしています。私は私のクラスのCharacter(ParentをPlayerクラスとEnemyクラスの2つのクラスに追加)を基本クラスGameObjectにしようとしています。文字のコンストラクタを作成しようとすると、C2512エラーがCharacter.cppファイルに発生します。誰かが私が間違っていることを指摘できますか?前もって感謝します。'GameObject':適切なデフォルトのコンストラクタがありません

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

答えて

0

あなたのGameObjectには、デフォルトのコンストラクタ(パラメータを持たないコンストラクタ)が必要です。

Character::Character()にあるため、コンパイラはGameObjectのデフォルトのコンストラクタを呼び出します。

クラスGameObjectのコンストラクタを実装していない場合、コンパイラはそのための空のデフォルトコンストラクタを生成します。しかし、クラスGameObjectのコンストラクタを実装しているので、コンパイラはそれをしません。あなた自身でデフォルトコンストラクタを提供する必要があります。

+0

は 'GameObject'が意図またはここに安全な行動であるにデフォルトコンストラクタを追加するようにしていません。私はR Sahuが意思のお金の上にあると思う。 – EyasSH

+0

助けてくれてありがとう。未解決の外部シンボルであるもう一つのより簡単なエラーが発生しました。それは通常あなたが何か正しいものを含んでいないときですか? – Zone

+0

@Zone新しいデフォルトコンストラクタを実装しましたか?あなたがそうしなければ、確かにそれは未解決のシンボルエラーを引き起こすでしょう。 –

1
Character::Character() {}; 

GameObjectは、デフォルトコンストラクタを持っていないので、コンパイラはエラーを生成

Character::Character() : 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) {} 
関連する問題