2016-10-31 48 views
-4

我已阅读有关使用列表和指向对象的指针的文章。但我无法弄清楚。我会分享我的文件。希望有人能帮助我。C++列表对象错误

具体的问题完全是我们的讲师如何写在董事会上。我拍了照片并使用它,但不起作用。对于这个问题,不容易找到解决方案。

该问题是第一个文件,Game.h。说每一个这样的下一个错误。在编译之前不会出现可见的错误。我究竟做错了什么 ?任何帮助非常感谢。我还的情况下添加下面的所有文件中的错误的列表中后的底部,他们帮助

GameObject* P_Player = new Player(); 
GameObject* P_Enemy_1 = new Enemy(); 
GameObject* P_Enemy_2 = new Enemy(); 
GameObject* P_Enemy_3 = new Enemy(); 
GameObject* P_Enemy_4 = new Enemy(); 

编译报道:

Severity Code Description Project File Line Suppression State 
Error  C2027 use of undefined type 'Player' MyGame c:\users\gaddmaster\desktop\c++\animated assignment\game\game.h 44 

Game.h

//Game.h 
#include "GameObject.h" 
#include "Player.h" 
#include "Enemy.h" 
#include <list> 
#include <cstdlib> 

using namespace std; 

class Game 
{ 
public: 
    Game(); 
    ~Game(); 
    void init(); 
private: 
    //Game objects, List and list iterator 
    list <GameObject*> listOfObjects; 
    list <GameObject*>::iterator listOfObjects_iterator; 


    GameObject* P_Player = new Player(); 
    GameObject* P_Enemy_1 = new Enemy(); 
    GameObject* P_Enemy_2 = new Enemy(); 
    GameObject* P_Enemy_3 = new Enemy(); 
    GameObject* P_Enemy_4 = new Enemy(); 
}; 

Game.cpp文件

//Game.cpp 
#include "Game.h" 
#include <array> 
#include <time.h> 
#include <string> 

Game::Game(){} 
Game::~Game(){} 

void Game::init() 
{ 
    int array[23][23]; 
    int randomInt; 
    srand(time(NULL)); 
    int serielID = 1; 

    int x; 
    int y; 
    for (x = 0; x < 24; x++) { for (y = 0; y < 24; y++) { array[x][x] = 0; } } 

    listOfObjects.push_back(P_Player); 
    listOfObjects.push_back(P_Enemy_1); 
    listOfObjects.push_back(P_Enemy_2); 
    listOfObjects.push_back(P_Enemy_3); 
    listOfObjects.push_back(P_Enemy_4); 

    for (listOfObjects_iterator = listOfObjects.begin(); listOfObjects_iterator != listOfObjects.end(); listOfObjects_iterator++) 
    { 
     do 
     { 
      x = rand() % 23; 
      y = rand() % 23; 
     }while (array[y][x] == 0); 
     x++;y++; 

     (*listOfObjects_iterator)->spawn(to_string(serielID), 100, 1, y, x); 
     serielID++; 
    } 
} 

GameObject.h

//GameObject.h 
#ifndef GAMEOBJECT_H 
#define GAMEOBJECT_H 
#include <iostream> 
#include <string> 
#include <list> 

using namespace std; 

class GameObject 
{ 
    public: 
     void spawn(string typeID, int health, int speed, int x, int y); 

    protected: 
     string m_typeID; 
     int m_health; 
     int m_speed; 
     signed int m_x; 
     signed int m_y; 
     bool alive; 
}; 

Player.h

//Player.h 
#include "GameObject.h" 
#include <list> 

using namespace std; 

class Player : public GameObject 
{ 

public: 
    Player(); 
    ~Player(); 


private: 
    int m_damage; 
    string player = "player"; 
}; 

Player.cpp

//Player.cpp 
#include "Player.h" 
#include <time.h> 

using namespace std; 

Player::Player(){} 

Player::~Player(){} 

Enemy.h

//Enemy.h 
#include "GameObject.h" 

using namespace std; 

class Enemy : public GameObject 
{ 

public: 
    Enemy(); 
    ~Enemy(); 


private: 
    int m_damage; 
    string enemy = "enemy"; 

}; 

个Enemy.cpp

//Enemy.cpp 
#include "Enemy.h" 
#include <time.h> 

using namespace std; 

Enemy::Enemy() {} 

Enemy::~Enemy() {} 

错误

Severity Code Description Project File Line Suppression State 
Error C2297 '-': illegal, right operand has type 'int (__thiscall Enemy::*)(void)' MyGame c:\users\gaddmaster\desktop\c++\animated assignment\game\game.cpp 124 
Error C2027 use of undefined type 'Player' MyGame c:\users\gaddmaster\desktop\c++\animated assignment\game\game.h 44 
Error C2027 use of undefined type 'Enemy' MyGame c:\users\gaddmaster\desktop\c++\animated assignment\game\game.h 45 
Error C2027 use of undefined type 'Enemy' MyGame c:\users\gaddmaster\desktop\c++\animated assignment\game\game.h 46 
Error C2027 use of undefined type 'Enemy' MyGame c:\users\gaddmaster\desktop\c++\animated assignment\game\game.h 47 
Error C2027 use of undefined type 'Enemy' MyGame c:\users\gaddmaster\desktop\c++\animated assignment\game\game.h 48 
Error C2107 illegal index, indirection not allowed MyGame c:\users\gaddmaster\desktop\c++\animated assignment\game\game.cpp 73 
Error C2107 illegal index, indirection not allowed MyGame c:\users\gaddmaster\desktop\c++\animated assignment\game\game.cpp 75 
Error C3867 'Player::giveDamage': non-standard syntax; use '&' to create a pointer to member MyGame c:\users\gaddmaster\desktop\c++\animated assignment\game\game.cpp 109 
Error C3867 'Player::giveDamage': non-standard syntax; use '&' to create a pointer to member MyGame c:\users\gaddmaster\desktop\c++\animated assignment\game\game.cpp 114 
Error C3867 'Player::giveDamage': non-standard syntax; use '&' to create a pointer to member MyGame c:\users\gaddmaster\desktop\c++\animated assignment\game\game.cpp 118 
Error C3867 'Player::giveDamage': non-standard syntax; use '&' to create a pointer to member MyGame c:\users\gaddmaster\desktop\c++\animated assignment\game\game.cpp 125 
Error C2011 'Player': 'class' type redefinition MyGame c:\users\gaddmaster\desktop\c++\animated assignment\game\player.h 6 
Error C3867 'GameObject::objectType': non-standard syntax; use '&' to create a pointer to member MyGame c:\users\gaddmaster\desktop\c++\animated assignment\game\game.cpp 72 
Error C3867 'GameObject::isAlive': non-standard syntax; use '&' to create a pointer to member MyGame c:\users\gaddmaster\desktop\c++\animated assignment\game\game.cpp 141 
Error C3867 'GameObject::getHealth': non-standard syntax; use '&' to create a pointer to member MyGame c:\users\gaddmaster\desktop\c++\animated assignment\game\game.cpp 109 
Error C3867 'GameObject::getHealth': non-standard syntax; use '&' to create a pointer to member MyGame c:\users\gaddmaster\desktop\c++\animated assignment\game\game.cpp 114 
Error C3867 'GameObject::getHealth': non-standard syntax; use '&' to create a pointer to member MyGame c:\users\gaddmaster\desktop\c++\animated assignment\game\game.cpp 118 
Error C3867 'GameObject::getHealth': non-standard syntax; use '&' to create a pointer to member MyGame c:\users\gaddmaster\desktop\c++\animated assignment\game\game.cpp 124 
Error C3867 'GameObject::GameObject_Y': non-standard syntax; use '&' to create a pointer to member MyGame c:\users\gaddmaster\desktop\c++\animated assignment\game\game.cpp 52 
Error C3867 'GameObject::GameObject_Y': non-standard syntax; use '&' to create a pointer to member MyGame c:\users\gaddmaster\desktop\c++\animated assignment\game\game.cpp 53 
Error C3867 'GameObject::GameObject_Y': non-standard syntax; use '&' to create a pointer to member MyGame c:\users\gaddmaster\desktop\c++\animated assignment\game\game.cpp 54 
Error C3867 'GameObject::GameObject_Y': non-standard syntax; use '&' to create a pointer to member MyGame c:\users\gaddmaster\desktop\c++\animated assignment\game\game.cpp 55 
Error C3867 'GameObject::GameObject_Y': non-standard syntax; use '&' to create a pointer to member MyGame c:\users\gaddmaster\desktop\c++\animated assignment\game\game.cpp 56 
Error C3867 'GameObject::GameObject_Y': non-standard syntax; use '&' to create a pointer to member MyGame c:\users\gaddmaster\desktop\c++\animated assignment\game\game.cpp 73 
Error C3867 'GameObject::GameObject_Y': non-standard syntax; use '&' to create a pointer to member MyGame c:\users\gaddmaster\desktop\c++\animated assignment\game\game.cpp 75 
Error C3867 'GameObject::GameObject_X': non-standard syntax; use '&' to create a pointer to member MyGame c:\users\gaddmaster\desktop\c++\animated assignment\game\game.cpp 52 
Error C3867 'GameObject::GameObject_X': non-standard syntax; use '&' to create a pointer to member MyGame c:\users\gaddmaster\desktop\c++\animated assignment\game\game.cpp 53 
Error C3867 'GameObject::GameObject_X': non-standard syntax; use '&' to create a pointer to member MyGame c:\users\gaddmaster\desktop\c++\animated assignment\game\game.cpp 54 
Error C3867 'GameObject::GameObject_X': non-standard syntax; use '&' to create a pointer to member MyGame c:\users\gaddmaster\desktop\c++\animated assignment\game\game.cpp 55 
Error C3867 'GameObject::GameObject_X': non-standard syntax; use '&' to create a pointer to member MyGame c:\users\gaddmaster\desktop\c++\animated assignment\game\game.cpp 56 
Error C2660 'Game::battle': function does not take 0 arguments MyGame c:\users\gaddmaster\desktop\c++\animated assignment\game\main.cpp 69 
Error C3867 'Enemy::giveDamage': non-standard syntax; use '&' to create a pointer to member MyGame c:\users\gaddmaster\desktop\c++\animated assignment\game\game.cpp 109 
Error C3867 'Enemy::giveDamage': non-standard syntax; use '&' to create a pointer to member MyGame c:\users\gaddmaster\desktop\c++\animated assignment\game\game.cpp 114 
Error C3867 'Enemy::giveDamage': non-standard syntax; use '&' to create a pointer to member MyGame c:\users\gaddmaster\desktop\c++\animated assignment\game\game.cpp 118 
Error C3867 'Enemy::giveDamage': non-standard syntax; use '&' to create a pointer to member MyGame c:\users\gaddmaster\desktop\c++\animated assignment\game\game.cpp 124 
Error C2011 'Enemy': 'class' type redefinition MyGame c:\users\gaddmaster\desktop\c++\animated assignment\game\enemy.h 5 
Error C2446 '==': no conversion from 'const char *' to 'std::string (__thiscall GameObject::*)(void)' MyGame c:\users\gaddmaster\desktop\c++\animated assignment\game\game.cpp 72 
Error C2446 '<=': no conversion from 'int (__thiscall Player::*)(void)' to 'int' MyGame c:\users\gaddmaster\desktop\c++\animated assignment\game\game.cpp 109 
Error C2446 '<=': no conversion from 'int (__thiscall Player::*)(void)' to 'int' MyGame c:\users\gaddmaster\desktop\c++\animated assignment\game\game.cpp 114 
Error C2446 '<=': no conversion from 'int (__thiscall Player::*)(void)' to 'int' MyGame c:\users\gaddmaster\desktop\c++\animated assignment\game\game.cpp 118 
Error C2297 '<=': illegal, right operand has type 'int (__thiscall Enemy::*)(void)' MyGame c:\users\gaddmaster\desktop\c++\animated assignment\game\game.cpp 109 
Error C2297 '<=': illegal, right operand has type 'int (__thiscall Enemy::*)(void)' MyGame c:\users\gaddmaster\desktop\c++\animated assignment\game\game.cpp 114 
Error C2297 '<=': illegal, right operand has type 'int (__thiscall Enemy::*)(void)' MyGame c:\users\gaddmaster\desktop\c++\animated assignment\game\game.cpp 118 
Error C2296 '<=': illegal, left operand has type 'int (__thiscall Enemy::*)(void)' MyGame c:\users\gaddmaster\desktop\c++\animated assignment\game\game.cpp 109 
Error C2296 '<=': illegal, left operand has type 'int (__thiscall Enemy::*)(void)' MyGame c:\users\gaddmaster\desktop\c++\animated assignment\game\game.cpp 114 
Error C2296 '<=': illegal, left operand has type 'int (__thiscall Enemy::*)(void)' MyGame c:\users\gaddmaster\desktop\c++\animated assignment\game\game.cpp 118 
Error C2297 '-': illegal, right operand has type 'int (__thiscall Player::*)(void)' MyGame c:\users\gaddmaster\desktop\c++\animated assignment\game\game.cpp 125 
+1

Crom这是很多代码。投注你可以工作到[mcve]。您不仅可以在制作MCVE时自己发现错误,但您会学到很多东西。 – user4581301

+1

强烈建议使用某种[Include Guard](https://en.wikipedia.org/wiki/Include_guard) – user4581301

+0

好吧,我刚刚阅读了关于最小,完整和可验证的事情。我现在要把它重新制作得更小。非常感谢您的建议。我会在10分钟后回复编辑 – Daniel

回答

0

首先,你调用一些吸气剂的特性(例如:player->getHealthgame.cpp:109)。 这是一种方法,所以你需要括号(例如:player->getHealth())。

然后加头文件保护您的.h文件,像这样:

#ifndef FILENAME_H_ 
#define FILENAME_H_ 

// header file content here 

#endif // !FILENAME_H_ 

您可以修改了一下语法,但把好文件名中宏。这将防止加倍包含头文件。

做完之后,如果有错误,请更新您的帖子,现在有太多的错误。

+0

非常感谢,现在就在这里工作 – Daniel

+0

Ya哇在功能结束时错过了很多(),完全忘了它们。由于某些原因,Virtual Studio具有弹出功能列表的功能,以便从中节省时间输入,并向我们展示可用的功能。但它遗漏了(),所以这就是为什么我一直想念他们,我认为 – Daniel

+0

哈哈就是这样,在守卫和添加()之间。谢啦。我要完成制作最小版本 – Daniel