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|
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)
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.
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.