c++列表对象错误
C++ List Objects Error
我读过关于使用列表和指针指向对象的文章。然而,我不明白。我会分享我的文件。希望有人能帮助我。
具体的问题是如何做我们的讲师写的板。我拍了照片,正在使用它,但不工作。要找到解决这个问题的办法并不容易。
问题是第一个文件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:usersgaddmasterdesktopc++animated assignmentgamegame.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:usersgaddmasterdesktopc++animated assignmentgamegame.cpp 124
Error C2027 use of undefined type 'Player' MyGame c:usersgaddmasterdesktopc++animated assignmentgamegame.h 44
Error C2027 use of undefined type 'Enemy' MyGame c:usersgaddmasterdesktopc++animated assignmentgamegame.h 45
Error C2027 use of undefined type 'Enemy' MyGame c:usersgaddmasterdesktopc++animated assignmentgamegame.h 46
Error C2027 use of undefined type 'Enemy' MyGame c:usersgaddmasterdesktopc++animated assignmentgamegame.h 47
Error C2027 use of undefined type 'Enemy' MyGame c:usersgaddmasterdesktopc++animated assignmentgamegame.h 48
Error C2107 illegal index, indirection not allowed MyGame c:usersgaddmasterdesktopc++animated assignmentgamegame.cpp 73
Error C2107 illegal index, indirection not allowed MyGame c:usersgaddmasterdesktopc++animated assignmentgamegame.cpp 75
Error C3867 'Player::giveDamage': non-standard syntax; use '&' to create a pointer to member MyGame c:usersgaddmasterdesktopc++animated assignmentgamegame.cpp 109
Error C3867 'Player::giveDamage': non-standard syntax; use '&' to create a pointer to member MyGame c:usersgaddmasterdesktopc++animated assignmentgamegame.cpp 114
Error C3867 'Player::giveDamage': non-standard syntax; use '&' to create a pointer to member MyGame c:usersgaddmasterdesktopc++animated assignmentgamegame.cpp 118
Error C3867 'Player::giveDamage': non-standard syntax; use '&' to create a pointer to member MyGame c:usersgaddmasterdesktopc++animated assignmentgamegame.cpp 125
Error C2011 'Player': 'class' type redefinition MyGame c:usersgaddmasterdesktopc++animated assignmentgameplayer.h 6
Error C3867 'GameObject::objectType': non-standard syntax; use '&' to create a pointer to member MyGame c:usersgaddmasterdesktopc++animated assignmentgamegame.cpp 72
Error C3867 'GameObject::isAlive': non-standard syntax; use '&' to create a pointer to member MyGame c:usersgaddmasterdesktopc++animated assignmentgamegame.cpp 141
Error C3867 'GameObject::getHealth': non-standard syntax; use '&' to create a pointer to member MyGame c:usersgaddmasterdesktopc++animated assignmentgamegame.cpp 109
Error C3867 'GameObject::getHealth': non-standard syntax; use '&' to create a pointer to member MyGame c:usersgaddmasterdesktopc++animated assignmentgamegame.cpp 114
Error C3867 'GameObject::getHealth': non-standard syntax; use '&' to create a pointer to member MyGame c:usersgaddmasterdesktopc++animated assignmentgamegame.cpp 118
Error C3867 'GameObject::getHealth': non-standard syntax; use '&' to create a pointer to member MyGame c:usersgaddmasterdesktopc++animated assignmentgamegame.cpp 124
Error C3867 'GameObject::GameObject_Y': non-standard syntax; use '&' to create a pointer to member MyGame c:usersgaddmasterdesktopc++animated assignmentgamegame.cpp 52
Error C3867 'GameObject::GameObject_Y': non-standard syntax; use '&' to create a pointer to member MyGame c:usersgaddmasterdesktopc++animated assignmentgamegame.cpp 53
Error C3867 'GameObject::GameObject_Y': non-standard syntax; use '&' to create a pointer to member MyGame c:usersgaddmasterdesktopc++animated assignmentgamegame.cpp 54
Error C3867 'GameObject::GameObject_Y': non-standard syntax; use '&' to create a pointer to member MyGame c:usersgaddmasterdesktopc++animated assignmentgamegame.cpp 55
Error C3867 'GameObject::GameObject_Y': non-standard syntax; use '&' to create a pointer to member MyGame c:usersgaddmasterdesktopc++animated assignmentgamegame.cpp 56
Error C3867 'GameObject::GameObject_Y': non-standard syntax; use '&' to create a pointer to member MyGame c:usersgaddmasterdesktopc++animated assignmentgamegame.cpp 73
Error C3867 'GameObject::GameObject_Y': non-standard syntax; use '&' to create a pointer to member MyGame c:usersgaddmasterdesktopc++animated assignmentgamegame.cpp 75
Error C3867 'GameObject::GameObject_X': non-standard syntax; use '&' to create a pointer to member MyGame c:usersgaddmasterdesktopc++animated assignmentgamegame.cpp 52
Error C3867 'GameObject::GameObject_X': non-standard syntax; use '&' to create a pointer to member MyGame c:usersgaddmasterdesktopc++animated assignmentgamegame.cpp 53
Error C3867 'GameObject::GameObject_X': non-standard syntax; use '&' to create a pointer to member MyGame c:usersgaddmasterdesktopc++animated assignmentgamegame.cpp 54
Error C3867 'GameObject::GameObject_X': non-standard syntax; use '&' to create a pointer to member MyGame c:usersgaddmasterdesktopc++animated assignmentgamegame.cpp 55
Error C3867 'GameObject::GameObject_X': non-standard syntax; use '&' to create a pointer to member MyGame c:usersgaddmasterdesktopc++animated assignmentgamegame.cpp 56
Error C2660 'Game::battle': function does not take 0 arguments MyGame c:usersgaddmasterdesktopc++animated assignmentgamemain.cpp 69
Error C3867 'Enemy::giveDamage': non-standard syntax; use '&' to create a pointer to member MyGame c:usersgaddmasterdesktopc++animated assignmentgamegame.cpp 109
Error C3867 'Enemy::giveDamage': non-standard syntax; use '&' to create a pointer to member MyGame c:usersgaddmasterdesktopc++animated assignmentgamegame.cpp 114
Error C3867 'Enemy::giveDamage': non-standard syntax; use '&' to create a pointer to member MyGame c:usersgaddmasterdesktopc++animated assignmentgamegame.cpp 118
Error C3867 'Enemy::giveDamage': non-standard syntax; use '&' to create a pointer to member MyGame c:usersgaddmasterdesktopc++animated assignmentgamegame.cpp 124
Error C2011 'Enemy': 'class' type redefinition MyGame c:usersgaddmasterdesktopc++animated assignmentgameenemy.h 5
Error C2446 '==': no conversion from 'const char *' to 'std::string (__thiscall GameObject::* )(void)' MyGame c:usersgaddmasterdesktopc++animated assignmentgamegame.cpp 72
Error C2446 '<=': no conversion from 'int (__thiscall Player::* )(void)' to 'int' MyGame c:usersgaddmasterdesktopc++animated assignmentgamegame.cpp 109
Error C2446 '<=': no conversion from 'int (__thiscall Player::* )(void)' to 'int' MyGame c:usersgaddmasterdesktopc++animated assignmentgamegame.cpp 114
Error C2446 '<=': no conversion from 'int (__thiscall Player::* )(void)' to 'int' MyGame c:usersgaddmasterdesktopc++animated assignmentgamegame.cpp 118
Error C2297 '<=': illegal, right operand has type 'int (__thiscall Enemy::* )(void)' MyGame c:usersgaddmasterdesktopc++animated assignmentgamegame.cpp 109
Error C2297 '<=': illegal, right operand has type 'int (__thiscall Enemy::* )(void)' MyGame c:usersgaddmasterdesktopc++animated assignmentgamegame.cpp 114
Error C2297 '<=': illegal, right operand has type 'int (__thiscall Enemy::* )(void)' MyGame c:usersgaddmasterdesktopc++animated assignmentgamegame.cpp 118
Error C2296 '<=': illegal, left operand has type 'int (__thiscall Enemy::* )(void)' MyGame c:usersgaddmasterdesktopc++animated assignmentgamegame.cpp 109
Error C2296 '<=': illegal, left operand has type 'int (__thiscall Enemy::* )(void)' MyGame c:usersgaddmasterdesktopc++animated assignmentgamegame.cpp 114
Error C2296 '<=': illegal, left operand has type 'int (__thiscall Enemy::* )(void)' MyGame c:usersgaddmasterdesktopc++animated assignmentgamegame.cpp 118
Error C2297 '-': illegal, right operand has type 'int (__thiscall Player::* )(void)' MyGame c:usersgaddmasterdesktopc++animated assignmentgamegame.cpp 125
首先,调用一些getter作为属性(例如:game.cpp:109
中的player->getHealth
)。这是一个方法,所以你需要括号(例如:player->getHealth()
)。
然后在.h
文件中添加Header Guard,如下所示:
#ifndef FILENAME_H_
#define FILENAME_H_
// header file content here
#endif // !FILENAME_H_
你可以稍微修改一下语法,但是把好的文件名放在宏中。这将防止重复包含头文件。
完成后,如果有一些错误,请更新您的帖子,现在有太多了。
相关文章:
- 在c++中尝试对对象数组进行排序时,出现std:bad_alloc错误
- 模板,函数使用错误的构造函数来复制我的对象
- 有没有办法让编译器在我放置字符串而不是 nlohmann::json 对象时抛出错误?
- std::unordered_map 类型对象声明期间出现"field has incomplete type"错误
- 错误:malloc:对象 0x7f9edf504080 的 *** 错误:未分配正在释放的指针
- 从对象C++中的文件读取内容时出现分段错误
- cout 新创建的对象引发运行时错误
- 错误:"释放后使用包装器对象 (WRAPPER_ESCAPE)"
- 使用返回对象的函数处理错误
- 创建 QtChart 对象会在运行时创建EXC_BAD_ACCESS错误
- 错误:无法传递非平凡类型"std::string"的对象和更多错误
- 该对象具有与成员函数不兼容的类型限定符.为什么会出现此错误?
- 如何在类模板的成员函数中正确调用函数对象?正在生成 Visual Studio 编译器错误 C2440
- 尝试将对象插入空指针数组时出现分段错误
- 错误:三元运算符无法在对象中正常工作"cout"
- C++:如果括号为空,为什么不抛出错误对象?
- qt vsaddin错误对象引用未设置为对象的istanse
- 如何从函数返回成功或错误对象?
- 复制构造函数错误:对象的类型限定符与成员函数不兼容
- 如何在本机插件中创建node.js错误对象