Sign in to follow this  

Cyclic Dependency Trouble

This topic is 4546 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

Hi Before I start I have read the article 'Organizing Code Files in C and C++ ' on this site and googled without any luck I have 2 classes Board and Referee both with references to one another.
Board

class Referee;

class Board
{
public:

	void Board::Init (int width, int height);
	ChessPiece* Board::GetPiece(int x, int y);
	void Board::SelectPiece();
	Referee* ref;
	POINTS** p;
};

void Board::Init (int width, int height)
{
	ref.Init(this);
}
void Board::SelectPiece()
{
	p = ref->FindPositions(SelectedPiece, x, y);
}
Referee

#include<Windows.h>
class Board;

class Referee
{
public:
	void Referee::Init(Board* b);
	POINTS** Referee::FindPositions(ChessPiece* piece, int x, int y);
	Board* board;
	
};

void Referee::Init(Board* b)
{
	board = b;
	.....
}

POINTS** Referee::FindPositions(ChessPiece* piece, int x, int y)
{
	....
	if(!board->GetPiece(x, y+(-1* dir)))
	....
}
As you can see I am trying to use forward declarations instead if including the header files which throws up a load of errors but I am getting a load of errors anyway. Errors like error C2079: 'Board::ref' uses undefined class 'Referee' error C2227: left of '->Init' must point to class/struct/union/generic type error C2039: 'GetPiece' : is not a member of 'Board' error C2027: use of undefined type 'Board' error C2227: left of '->GetPiece' must point to class/struct/union/generic type can someone please explain where i am going wrong

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
void Board::Init (int width, int height)
{
ref.Init(this);
}
void Board::SelectPiece()
{
p = ref->FindPositions(SelectedPiece, x, y);
}

is in a different file from

class Board
{
public:

void Board::Init (int width, int height);
ChessPiece* Board::GetPiece(int x, int y);
void Board::SelectPiece();
Referee* ref;
POINTS** p;
};

right?

Share this post


Link to post
Share on other sites
yea its in a different file I just added it there to save space in the post.

I figured it out anyway. Forward declarations are to allow you to declare a class in the header file. You still include the h files in the source file.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Quote:
Original post by clearz
yea its in a different file I just added it there to save space in the post.

I figured it out anyway. Forward declarations are to allow you to declare a class in the header file. You still include the h files in the source file.


this is what i was getting to ;)

Share this post


Link to post
Share on other sites

This topic is 4546 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.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this