Daniel Daniel - 1 month ago 9
C++ Question

C++ List Objects Error

I have read posts about using lists and pointer to objects. However I cannot figure it out. I will share my files. Hopefully someone can help me. The specific problem is done exactly how our lecturer wrote of board. I took picture and am using it, but doesn't work. Its not easy find solution to exactly the issue. The issue is the first file, Game.h. Says next error for each one of these. No visible errors appear before compiling. What am I doing wrong ? Any help much appreciated. I have also added list of errors below all files at bottom of post in case they help
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 file

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


Errors

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

Answer

Firstly, you call some getter as a property (ex: player->getHealth in game.cpp:109). It's a method, so you need parenthesis (ex: player->getHealth()).

Then add Header Guard in your .h files, like this:

#ifndef FILENAME_H_
#define FILENAME_H_

// header file content here

#endif // !FILENAME_H_

You can modify a bit the syntax, but put the good filename in the macro. This will prevent to double include a header file.

After doing it, update your post if there are some errors, there are too many right now.