linking probleempje?

Status
Niet open voor verdere reacties.

Murdocki

Gebruiker
Lid geworden
7 jun 2007
Berichten
449
ik heb het volgende probleem, als ik twee klassen aan elkaar wil koppelen dan krijg ik de volgende fouten:

d:\echoengine\source\echoengine\kernel.h(14) : error C2143: syntax error : missing ';' before '*'
d:\echoengine\source\echoengine\kernel.h(14) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
d:\echoengine\source\echoengine\kernel.h(14) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int


bovenstaande gebeurt alleen als ik de include van Kernel.h in EntityManager.h aan zet.

de headers zijn:

Code:
[B]Engine.h[/B]
#ifndef _EE_ENGINE_H_
#define _EE_ENGINE_H_

#include "Kernel.h"

class Engine
{
public:
	Engine();
	~Engine();
	bool shutdown();
private:
	Kernel* kernel;
};

#endif
Code:
[B]Kernel.h[/B]
#ifndef _EE_KERNEL_H_
#define _EE_KERNEL_H_
#include "EntityManager.h"
#include "SceneManager.h"

class Kernel
{
public:
	Kernel();
	~Kernel();
	bool initialize();
	bool shutdown();
private:
	EntityManager* entityManager;
	SceneManager* sceneManager;
};

#endif
Code:
[B]EntityManager.h[/B]
#ifndef _EE_ENTITYMANAGER_H_
#define _EE_ENTITYMANAGER_H_

//#include "Kernel.h"

class EntityManager
{
public:
	EntityManager(/* Kernel* kernel */)
		/*: kernel( kernel )*/;
	~EntityManager();
	bool initialize();
	bool shutdown();
private:
	//Kernel* kernel;
};

#endif

dus weet iemand of ik iets verkeerd aan elkaar link of dat ik iets anders fout doe?
 
Kernel heeft een pointer naar EntityManager en EntityManager een pointer naar Kernel.

http://www.gamedev.net/reference/articles/article1798.asp

Cyclic (or two-way) dependencies are a common problem in software engineering. Many constructs involve a two-way link of some sort, and this implies that both classes or structures know about each other. Often this ends up looking like this:

/* Parent.h */
#include "child.h"
class Parent
{
Child* theChild;
};

/* Child.h */
#include "parent.h"
class Child
{
Parent* theParent;
};

Given that one of these has to be compiled first, you need some way to break the cycle. In this case, it's actually quite trivial. The Parent struct doesn't actually need to know the details of the Child class, as it only stores a pointer to one. Pointers are pretty much the same no matter what they point to, therefore you don't need to the definition of the structure or class in order to store a pointer to an instance of that structure or class. So the #include line is not needed. However, simply taking it out will give you an "undeclared identifier" error when it encounters the word 'Child', so you need to let the compiler know that Child is a class or class that you wish to point to. This is done with a forward declaration, taking the form of a class or class definition without a body. Example:

/* Parent.h */
class Child; /* Forward declaration of Child; */
class Parent
{
Child* theChild;
};

Notice how the #include line is replaced by the forward declaration. This has allowed you to break the dependency between Parent.h and Child.h. Additionally, it will speed up compilation as you are reading in one less header file. In this case, the same procedure can (and should) be followed in Child.h by forward declaring "class Parent;" As long as you are only referring to a pointer and not the actual type itself, you don't need to #include the full definition. In 99% of cases, this can be applied to one or both sides of a cycle to remove the need to #include one header from another, eliminating the cyclic dependency.

Of course, in the source files, it's quite likely that there will be functions that apply to Parent that will manipulate the Child also, or vice versa. Therefore, it is probably necessary to #include both parent.h and child.h in parent.c and child.c.

^^
 
allright dat was de oplossing dus, bedankt voor de hulp :D

alleen nu zie ik misschien een ander probleem (waar ik op dit moment geen last van heb), als je nou een abstracte klasse maakt kan je dan de implementatie ook apart houden van de declaratie? anders kom je misschien wel in de knel omdat je geen methodes van een bepaald object kent.
 
Laatst bewerkt:
alleen nu zie ik misschien een ander probleem (waar ik op dit moment geen last van heb), als je nou een abstracte klasse maakt kan je dan de implementatie ook apart houden van de declaratie? anders kom je misschien wel in de knel omdat je geen methodes van een bepaald object kent.

In zo'n base klasse stop je natuurlijk alleen de dingen die echt relevant zijn. In de afgeleide klasses kun je dan de overige zaken stoppen die nodig zijn.
 
ja ok maar dat bedoel ik niet, ik bedoel meer wat je nou moet doen als je de implementatie van een bepaalde methode in de .h wilt hebben
 
ja ok maar dat bedoel ik niet, ik bedoel meer wat je nou moet doen als je de implementatie van een bepaalde methode in de .h wilt hebben

Als je de implementatie in de header wilt hebben dan stop je die toch in de header. Gaat toch zonder problemen ?
 
tenzij de implementatie vereist dat je een methode van een ander object aan roept
 
zelfs zonder de header van de andere klasse geincluded te hebben dan? ik denk nu namelijk dat hij dan heel die methodes niet kent omdat je de klasse met een "forward declaration" gedeclareed hebt en hier niet de methodes die je aan roept in staan
 
zelfs zonder de header van de andere klasse geincluded te hebben dan? ik denk nu namelijk dat hij dan heel die methodes niet kent omdat je de klasse met een "forward declaration" gedeclareed hebt en hier niet de methodes die je aan roept in staan

Als je een forward declaration gebruikt dan is er toch gewoon een reference/pointer naar die klasse, dus dan zijn ook de eigenschappen van die klasse beschikbaar.
 
Status
Niet open voor verdere reacties.
Terug
Bovenaan Onderaan