## Recommended Posts

frogtag    121
I'll try to keep this simple.. I have a Main.cpp that calls a player class, defined in Character.cpp. The class defines the main player and (eventually) NPC attributes, such as x, y & z world coordinates and facing direction. The problem is that when i try to access the class atributes (for example using Player.xCoord) in a movement(.cpp) file, using a movement function, it tells me that the Player class is undefined. I think that i've got to either.. - define the player class again in the movement.cpp but declare it as external (i've tried this but can't get it to work) or.. - pass a pointer to the movement.cpp function from main.cpp (where the player class was orignally defined, but this seems overly complicated) or.. - pass a variable to the movement.cpp function from main.cpp, do the math and then return a value that is then put into Player.xCoord (a going-around-the-houses method, that again seems overly complicated) Can anyone tell me what i'm doing wrong? Should i be even using a class? (i think i should as in the long run it'll keep the number of variables down and tidy.) Is a structure better?

##### Share on other sites
SiCrane    11839
In C++ classes and structures are the same (except that structs have default public members and inheritance and classes have default private), so changning to a struct wouldn't help. I'm not entirely sure what your code looks like, so I can't give anything more than vague advice, but it seems like you should be defining the class in a header file and including that in the various source files. For other ideas of what might be wrong see this article.

##### Share on other sites
frogtag    121
Thanks for clearing up the class and structure question.

If i stick the class call in a seperate header file then link to it in main.cpp and movement.cpp will this solve my undeclared problem?

(CODE FROM MEMORY AND BASIC - IT WILL HAVE ERRORS, BUT TRY TO IGNORE THEM. JUST TRYING TO UNDERSTAND THE THEORY)

classdec.h
...using namespace charcharacter Player;...

main.cpp
#include ...#include "classdec.h"//using namespace char       - NO LONGER NEEDED//character Player;          - NO LONGER NEEDED...Foward();...

movement.cpp
#include "classdec.h"...Foward(){Player.xCoord+=1;}

character.cpp
#include "character.h"using namespace char{  Class character  {    character:Character()    {};    ~character:Character()    {};  };}

character.h
using namespace char{  Class character  {  character:Character()  {  xCoord = 0.0f  };  ~character:Character()  {}}

##### Share on other sites
Enigma    1410
You could put the code that accesses the class member in a separate function in a separate file, but that would seem to be overkill and you would then need a forward declaration of the function in your main file anyway. The standard mechanism for solving these kinds of problems is:
// character.hnamespace chars // char is a reserved word{	// class definition	class character	{		public:			// class member declarations			character();			~character();			int xCoord;			// more stuff	};}// character.cpp#include "character.h"namespace chars{	// class member definitions	character::character()	{		// stuff	}	character::~character()	{		// stuff	}}// main.cpp#include "character.h"int main(){	character mycharacter;	// class member access	mycharacter.xCoord = 7;}

To access a class member you need to have included the class definition. Likewise, to call a member function (including constructors and destructors) you also need to have included the class definition. If all you are doing is passing an object then you only need a class declaration, i.e.:
namespace chars{	// class declaration	class character;}void myfunction(character & c){	someotherfunction(c);}

So in general, if you mention a class you need to have the class declaration. If you use a class you need to have the class definition. Class declarations can occur multiple times within the same source file with no error. Class definitions can only occur once within each source file but can occur in multiple source files (but must match). Since class member declarations are contained within class definitions they follow the same rules as class definitions. Finally class member definitions must only occur once in the entire program.

Of course, rather then modifying the member variables it would be a better idea to encapsulate that functionality and provide character with a forward member function.

Enigma