我已閱讀有關使用列表和指向對象的指針的文章。但我無法弄清楚。我會分享我的文件。希望有人能幫助我。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
Crom這是很多代碼。投注你可以工作到[mcve]。您不僅可以在製作MCVE時自己發現錯誤,但您會學到很多東西。 – user4581301
強烈建議使用某種[Include Guard](https://en.wikipedia.org/wiki/Include_guard) – user4581301
好吧,我剛剛閱讀了關於最小,完整和可驗證的事情。我現在要把它重新制作得更小。非常感謝您的建議。我會在10分鐘後回覆編輯 – Daniel