Skip to main content
GameDev.net gamedev.net
🔒 Locked

Linking Classes PLEASE HELP

Started by White Spider Oct 1, 2008 at 12:20 PM 3 replies 550+ views
Original Post
White Spider
White Spider
Hi I'm at my wits end with this problem. Put simply, I have a load of classes in source and header files. What I am finding on multiple occasions is I declare a class in a .h, and one of its variables is another class. No problem, right?? I make sure to include the .h of the class I will use, and create a variable for it, e.g #include "Node.h" ... class PlayerManager { public: ........ private: Node *m_node; }; When I compile it I get the error: error C2143: syntax error : missing ';' before '*' on the line where I declare m_node. but WHY????? I'm sure that there must be a simple answer, but I can't see it. HELP ME
popsoftheyear
popsoftheyear
If Node.h includes some other class that includes PlayerManager.h before Node.h actually declares the Node class, this can happen. There are lotsa fun issues with circular dependancy between header files.

Here's an easy fix for the specific situation you posted though. Since you have a pointer to Node in your PlayerManager class, you don't actually need to include Node's full declaration from Node.h. IF you had "Node m_node", than you would definitely need the full declaration so the compiler could know things like the size of Node to know what the size of PlayerManager will be. Since you just have a pointer to it, all the compiler needs to know is that it is a pointer, and that there is a valid class name called "Node" before the pointer.

Soooo in PlayerManager.h, instead of "#include "Node.h"", write "class Node;". This will pre-declare the class called Node, telling the compiler that "Node *m_node" is valid, but doesn't give it ANY more information about the class called Node. Just make sure in PlayerManager.cpp, you DO #include "Node.h" since it will need to know more about it then, assuming you use it.

In fact it is good to do this in all your class header files that use pointers to other classes. No need to include the whole class declaration... just pre-declare it, and include the full declaration in your cpp file.

Cheers
-Scott
popsoftheyear
popsoftheyear
Also remember to put inclusion guards around your headers. "#pragma once" will work in many compilers.

Cheers
-Scott
Perost
Perost
There is nothing wrong with that code, so you need to show more code (the content of Node.h would be a good start). In this case it's not necessary to include Node.h however, since you only use a pointer to Node. A simple forward declaration of Node can be used, since the definition of PlayerManager is not dependent on the definition of Node. This can help to speed up the compilation, so you should always use forward declarations whenever possible. An example:

//PlayerManager.hclass Node; //Forward declarationclass PlayerManager{....    private:         Node *m_node;}; //PlayerManager.cpp#include "Node.h" //The implementation of PlayerManager needs to know the interface of Node to be able to use m_node.#include "PlayerManager.h"//Implementation of PlayerManager
White Spider
White Spider
Pre-declaration worked a treat.

Thanks a lot guys

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.