Skip to main content
gamedev.net
🔒 Locked

Strange Problem

Started by Aidamina Sep 8, 2005 at 12:51 PM 5 replies 500+ views
Original Post
Aidamina
Aidamina
I got two classes: engine.h

#pragma once

class engine
{
public:
	content *  tcontent;

	engine(void);
	virtual ~engine(void);
};
content.h

#pragma once

class content
{
	engine * p_tengine;
public:
	content(void);
	virtual ~content(void);
};
And i can't get this to work, because of the way i include the header files. The engine is the main class and content is a member in it. the pointer to an engine class in the content class is a pointer to its parent. If i include content.h before engine.h it will create errors in content.h ( not recognizing engine class) and if i include engine.h before content.h it will create errors in engine.h ( not recognizing content class ) could really use the help. And yes the pointer to the parent are required!
-->My Site<--
KulSeran
KulSeran
Foward declaration is one solution.
Another is to rethink your design and remove the circular dependancy.

Foward declaration looks like this:

In content.h:
class engine; //notice this line. It defines engine without the need to define the members here.class content{	engine * p_tengine;public:	content(void);	virtual ~content(void);};

In engine.h:
#include "content.h"class engine{public:	content *  tcontent;	engine(void);	virtual ~engine(void);};
Aidamina
Aidamina
That would work any other suggestions any1?
-->My Site<--
Phantom Sage
Phantom Sage
I'd suggest trying to find a better way to structure your class(es). But I realize sometimes this approach is necessary/easier.

Here's what I'd do:

in prereq.h:
class content;class engine;


then within both the engine and content header files (be sure to remove any #includes to the engine/content header files):
#include "prereq.h"


And then include your engine/content header files in the engine/content.cpp files.
#include "content.h"#include "engine.h"// content constructorcontent::content(){...}


This may not be 100% accurate as I haven't messed with C++ in quite some time now, but the jist of it is still there.

But again, I would strongly suggest you consider restructuring your object design so that interdependencies like this aren't a problem.

streamer
streamer
put it in one header like this:

#ifndef _MY_HEADER_H_
#define _MY_HEADER_H_

class content;

class engine
{
public:
content * tcontent;

engine(void);
virtual ~engine(void);
};

class content
{
engine * p_tengine;
public:
content(void);
virtual ~content(void);
};

#endif
Sneftel
Sneftel
Quote:
Original post by streamer
put it in one header like this:

Why?

Aidamina, look here for everything you ever wanted to know about structuring your source code.
streamer
streamer
Quote:
Original post by Sneftel
Quote:
Original post by streamer
put it in one header like this:

Why?

Aidamina, look here for everything you ever wanted to know about structuring your source code.


yeah you are right [smile]. but on small classes I use metode above. Why split simple source code in two header?
But nevermind. If you are going to use nice programing style forget about my example [smile].

Topic Locked

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

Sign in to reply to this topic.