Having trouble storing a class pointer in std::map

Hello, I am currently attempting to teach myself C++. I am using Windows 7 64 bit, and Codeblocks with mingw. Though the original purpose was to create games, this code is just practice. I have an Entity class and an Entity Manager class. The function AddEntity() in EntityManager is supposed to take an Entity class as an argument and insert a pointer of it into a stl map(EntityContainer). Though I am having trouble with the initialization of the map and the function itself.

Just for clarity the purpose of the EntityManager class is so every Entity that is created, whether it is a cat, dog, or airplane can be stored in one central container with it's own unique ID. So each one can be easily tracked and managed.I also included Entity.h and Files.h since they are included by EntityManager.h.

My main issue occurs at the stl map EntityContainer's declaration at line 11 in EntityManager.h. I am not entirely sure if I should be using class& Entity or class* Entity, though both ways cause errors. I could also just put Entity, but then how would I know if the program thinks I am talking about the Entity class or just some undefined variable.

I am sorry for the long block of code, as this forum strangely does not have spoiler tags. Also, I tried to ask my question correctly, please let me know if I have left anything important out. Thank you.


C:\Users\Luke\Desktop\Projects\Project2\EntityManager.h|9|error: template argument 2 is invalid|
C:\Users\Luke\Desktop\Projects\Project2\EntityManager.h|9|error: template argument 4 is invalid|
C:\Users\Luke\Desktop\Projects\Project2\EntityManager.h|12|error: expected identifier before '&' token|
C:\Users\Luke\Desktop\Projects\Project2\EntityManager.h|18|error: 'int EntityManager::AddEntity' is not a static member of 'class EntityManager'|
C:\Users\Luke\Desktop\Projects\Project2\EntityManager.h|18|error: expected primary-expression before 'class'|
C:\Users\Luke\Desktop\Projects\Project2\EntityManager.h|19|error: expected ',' or ';' before '{' token|


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#ifndef ENTITYMANAGER_H_INCLUDED
#define ENTITYMANAGER_H_INCLUDED
#include "Files.h"
#include "Entity.h"

class EntityManager
{
public:
    std::map <int, class* Entity> EntityContainer;
    int CurrentIndividualID  = 1;

    int AddEntity(class& Entity);

private:

};

int EntityManager::AddEntity(class& Entity)
{

    EntityManager::EntityContainer.insert(std::map<int, class& Entity>::value_type(EntityManager::CurrentIndividualID, &Entity));
    EntityManager::CurrentIndividualID++;
}

#endif // ENTITYMANAGER_H_INCLUDED 


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
#ifndef ENTITY_H_INCLUDED
#define ENTITY_H_INCLUDED

#include "Files.h"

class Entity
{
    public:
    /*Entity(std::string& Name, int iID)
    {
        Entity::IName = iName;
        Entity::SetIndividualID(iID);
    }
    */
    Entity()
    {

    }
    ~Entity()
    {

    }

    int SetGlobalID(int& NewID);
    int GetGlobalID();
    int SetGlobalName(std::string& NewName);
    int GetGlobalName();

    int SetIndividualID(int& NewID);
    int GetIndividualID();
    int SetIndividualName(std::string& NewName);
    int GetIndividualName();

    protected:
    std::string GName; //Global Name (Nat needed yet)
    std::string* pGName = &GName;
    int GID = 0; //Global ID
    int* pGID = &GID;

    std::string IName; //Individual Name.
    std::string* pIName = &IName;
    int IID = 0; //Individual ID
    int* pIID = &IID;


};

int Entity::SetGlobalName(std::string& NewName)
{
    *Entity::pGName = NewName;
}

int Entity::GetGlobalName()
{
    std::cout << Entity::GName;
}

int Entity::SetGlobalID(int& NewID)
{
    *Entity::pGID = NewID;
}

int Entity::GetGlobalID()
{
    std::cout << Entity::GID;
}

int Entity::SetIndividualName(std::string& NewName)
{
    *Entity::pIName = NewName;
}

int Entity::GetIndividualName()
{
    std::cout << Entity::IName;
}

int Entity::SetIndividualID(int& NewID)
{
    *Entity::pIID = NewID;
}

int Entity::GetIndividualID()
{
    std::cout << Entity::IID;
}
#endif // ENTITY_H_INCLUDED 


1
2
3
4
5
6
7
8
9
10
#ifndef FILES_H_INCLUDED
#define FILES_H_INCLUDED

#include <vector>
#include <map>
#include <string>
#include <iostream>
#include <gl/glew.h>

#endif // FILES_H_INCLUDED 


I am not entirely sure if I should be using class& Entity or class* Entity
You probably want Entity *. class & identifier is illegal syntax in any context.

how would I know if the program thinks I am talking about the Entity class or just some undefined variable.
Because you declared a class named 'Entity' but not a variable named 'Entity'. If you hadn't declared the class, the compiler would not make any assumptions about what 'Entity' could refer to, and would just give an error.
If you had declared both a class and a variable named 'Entity', the compiler would accept the first one it encounters and reject the second one (in order of appearance in the preprocessor output)
Last edited on
Thank you Helios, that has fixed the problem. Though I have a new error. I searched for it and came up with issues of a missing bracket or misplaced statement and am not sure what mistake I made.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#ifndef ENTITYMANAGER_H_INCLUDED
#define ENTITYMANAGER_H_INCLUDED
#include "Files.h"
#include "Entity.h"

class EntityManager
{
public:
    std::map <int, Entity*> EntityContainer;
    int CurrentIndividualID  = 1;

    int AddEntity(class& Entity);

private:

};

int EntityManager::AddEntity(Entity*)
{
	EntityManager::EntityContainer.insert(std::map<int, Entity*>::value_type(EntityManager::CurrentIndividualID, &Entity));
	EntityManager::CurrentIndividualID++;
}

#endif // ENTITYMANAGER_H_INCLUDED  

The errors occur at line 20.

Expected primary-expression before '(' token.
Expected primary-expression before ')' token.


Thank you once again, I'll continue to search for a solution for this.
Last edited on
In no particular order:

Lines 20 and 21: That's not how you access an object's members. See http://www.cplusplus.com/doc/tutorial/classes/
Line 20: That's not how you insert items into a map.
1
2
std::map<int, std::string> map;
map[1] = "1";

Line 12: Again, class & Entity is not valid syntax.
Lines 12 and 18: The signatures of the declaration and definition of a member function must be the same.
Line 10: Except in very specific circumstances, you can't initialize class members in the class definition. You have to do it in the constructor.
Lines 18-22: It looks like you're putting the definition of this function in a header. For non-template classes, this is almost never what you want.
Thank you Helios. I forgot to correct line 12 (I code on a different computer and tried to remember the changes I made). Also, I may need to dump the book I am using for reference as it said to insert items into a map by that method. It is called "Sam's Teach Yourself C++ in One Hour a Day." or something along the lines of that, I know there are better books for learning C++. But, at this point I would rather just use online tutorials than buy a new book.

Is putting the definition and the declaration of a member function in separate .h and .cpp files required in some cases, or just a good standard to follow?

Edit: I have it working now, Thanks again for your help Helios.
Last edited on
Topic archived. No new replies allowed.