Home > Software engineering >  Passing a pointer object within a function in a header file
Passing a pointer object within a function in a header file

Time:03-19

I am experincing an issue where the error is: "2061 syntax error: identifier 'Person'

Enemy.h:

#include "person.h"

class Enemy
{
public:
    Enemy(string, string);

    void damage(Person*, Enemy*);
private:
    string name;
    string description;
};

Enemy.cpp:

Enemy::Enemy(string name, string description)
{
    this->name = name;
    this->description = description;
}

void Enemy::damage(Person* person, Enemy* enemy) 
{
    // do something
}

Person.h:

class Person
{
public:
    Person(string, string, int);

private:
    string name;
    string description;
    int health;

};

Person.cpp:

Person::Person(string name, string description, int health)
{
    this->name = name;
    this->description = description;
    this->health = health;
}

This line is causing the 206 error:

void damage(Person*, Enemy*);

Any help would be appreciated, thanks

CodePudding user response:

In Enemy.h you have a declaration void damage(Person*, Enemy*);, and in Enemy.cpp you have a definition void Enemy::doDamage(Person* person, Enemy* enemy). The names damage and doDamage are different.

CodePudding user response:

The code in Enemy.cpp needs to know what the complete Person class looks like in order to access its members. But, since the code in Enemy.h is using only Person* pointers, it just needs to know that the Person class exists but not what it looks like.

There are two ways to approach this:

Make Enemy.h include Person.h, and make Enemy.cpp include Enemy.h, eg:

Enemy.h:

#ifndef Enemy_H
#define Enemy_H

#include <string>
#include "Person.h"

class Enemy
{
public:
    Enemy(std::string, std::string);

    void damage(Person*, Enemy*);
private:
    std::string name;
    std::string description;
};

#endif

Enemy.cpp:

#include "Enemy.h"

Enemy::Enemy(std::string name, std::string description)
{
    this->name = name;
    this->description = description;
}

void Enemy::damage(Person* person, Enemy* enemy) 
{
    // do something
}

Person.h:

#ifndef Person_H
#define Person_H

#include <string>

class Person
{
public:
    Person(std::string, std::string, int);

private:
    std::string name;
    std::string description;
    int health;

};

#endif

Person.cpp:

#include "Person.h"

Person::Person(std::string name, std::string description, int health)
{
    this->name = name;
    this->description = description;
    this->health = health;
}

Alternatively, have Enemy.h forward declare Person, and then include Person.h in Enemy.cpp, eg:

Enemy.h:

#ifndef Enemy_H
#define Enemy_H

#include <string>

class Person;

class Enemy
{
public:
    Enemy(std::string, std::string);

    void damage(Person*, Enemy*);
private:
    std::string name;
    std::string description;
};

#endif

Enemy.cpp:

#include "Enemy.h"
#include "Person.h"

Enemy::Enemy(std::string name, std::string description)
{
    this->name = name;
    this->description = description;
}

void Enemy::damage(Person* person, Enemy* enemy) 
{
    // do something
}

Person.h:

#ifndef Person_H
#define Person_H

#include <string>

class Person
{
public:
    Person(std::string, std::string, int);

private:
    std::string name;
    std::string description;
    int health;

};

#endif

Person.cpp:

#include "Person.h"

Person::Person(std::string name, std::string description, int health)
{
    this->name = name;
    this->description = description;
    this->health = health;
}
  • Related