c++列表对象错误

C++ List Objects Error

本文关键字:错误 对象 列表 c++      更新时间:2023-10-16

我读过关于使用列表和指针指向对象的文章。然而,我不明白。我会分享我的文件。希望有人能帮助我。

具体的问题是如何做我们的讲师写的板。我拍了照片,正在使用它,但不工作。要找到解决这个问题的办法并不容易。

问题是第一个文件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_

你可以稍微修改一下语法,但是把好的文件名放在宏中。这将防止重复包含头文件。

完成后,如果有一些错误,请更新您的帖子,现在有太多了。