Vector of pointers from inside a class to another class

I'm developing in C++ a biological simulation: hunters and preys in a grid, with random-related cooperation.

For this, I have a grid of pointers to a class `Agent` and have classes `Prey` and `Hunter` that are also `Agent`.

I'm working in C++ and it throws me an error in my declaration of the vector of pointers to Prey* inside of my Hunter class. I'm working on Visual Studio 2017 and have tried with forward declaration and with putting all the files in a single header, but the error still prevents me from compiling.

Can someone point me to my mistake?

This is a reduced version of the structure of classes so far:

File: Environment.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include Agent.h
struct Position {
    int x;
    int y;
};

class Environment {
public:
    Environment();
    ~Environment();
private:
    Agent* grid[WORLDSIZEX][WORLDSIZEY];
};

enum AgentType { PREY, HUNTER };


File: Agent.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
class Agent {
public:
    Agent(Environment* aWorld, int xcoord, int ycoord);
    virtual ~Agent() { }
    virtual void move() = 0;
    virtual void breed() = 0;
    virtual AgentType getType() const = 0;

protected:
    void movesTo(int xNew, int yNew);
    int x;
    int y;
    Environment* world;

private:
};


File: Hunter.h
1
2
3
4
5
6
7
8
9
10
11
12
class Prey;
class Hunter : public Agent {
public:
    Hunter(Environment* aWorld, int xcoord, int ycoord);
    void move();
    void breed();
    AgentType getType() const;
    vector<Prey*> pList;
    bool altruistic;

private:
};


File: Prey.h
1
2
3
4
5
6
7
8
9
10
11
12
#include "Agent.h"

class Prey : public Agent {
public:
	void move();
	void breed();
	char representation() const;

private:
	int huntingDifficulty;
};


The errors appear in the output as:
1
2
3
4
1
1>environment.cpp
1>c:\users\eloyr_5y442ym\source\repos\the_proposed_project\hunter.h(36): error C2143: syntax error: missing ';' before '<'
1>c:\users\eloyr_5y442ym\source\repos\the_proposed_project\hunter.h(36): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\users\eloyr_5y442ym\source\repos\the_proposed_project\hunter.h(36): error C2238: unexpected token(s) preceding ';'


Thanks,
Eloy RD
Last edited on
Do you actually have a
#include <vector>
anywhere?

You may also need to qualify it:
std::vector<Prey*> pList;
@lastchance. Thanks a lot.

I do have included:
 
#include <iostream> 


But I was missing:
 
#include <vector> 


I wasn't aware of the requirement of "qualifying" inside of a header file. But of course, it now makes a lot of sense.

I modified the file to say:

1
2
3
4
5
6
7
8
9
10
11
12
class Prey;
class Hunter : public Agent {
public:
    Hunter(Environment* aWorld, int xcoord, int ycoord);
    void move();
    void breed();
    AgentType getType() const;
    std::vector<Prey*> pList;
    bool altruistic;

private:
};


And it compiles now.
Let's see what I break when I try to implement my smart pointers.
Last edited on
Topic archived. No new replies allowed.