Jump to content
  • Advertisement
Sign in to follow this  
Aidamina

Strange Problem

This topic is 4817 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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!

Share this post


Link to post
Share on other sites
Advertisement
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);

};




Share this post


Link to post
Share on other sites
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 constructor
content::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.

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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].

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!