Circlular Referencing Problem

Hey Guys, I am stuck at a part in my program where I can't continue because of an error stating that one of my pointers to a class does not name a type, therefore I am assuming there is a problem with my referencing.

In the bottom code, I think there is a circular referencing problem involving the following classes: PositionMover.h & SwimmingArea.h, I receive an error stating the following:

In file included from PositionMover.h:3:0,
from PositionMover.cpp:1:
SwimmingArea.h:15:8: error: ‘PositionMover’ does not name a type
PositionMover.cpp:3:29: error: expected constructor, destructor, or type conversion before ‘(’ token
make: *** [PositionMover.o] Error 1


Note that this is part of an assignment, so I need the code to work as presented, but how to solve the referencing problem.

My code looks as follows:
(Please note: I am aware that there might be minor errors in the code, however I can't continue to debug because of the current problem.)

Human.h (Just For Reference)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#ifndef HUMAN_H
#define HUMAN_H

class Human
{
	public:
		Human(char value, int x, int y);
		
		void setValue(char value);
		void setX(int x);
		void setY(int y);
		char getValue();
		int getX();
		int getY();
	private:
		char mValue;
		int mX;
		int mY;
};
#endif 


Human.cpp (Just For Reference)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#include "Human.h"

Human::Human(char value, int x, int y)
{
	mValue = value;
	mX = x;
	mY = y;
}

char Human::getValue()
{
	return this->mValue;
}

void Human::setValue(char value)
{
	mValue = value;
}

void Human::setX(int x)
{
	mX = x;
}

void Human::setY(int y)
{
	mY = y;
}

int Human::getX()
{
	return mX;
}

int Human::getY()
{
	return mY;
}


Surfer.h (Just For Reference)
1
2
3
4
5
6
7
8
9
10
#ifndef SURFER_H
#define SURFER_H
#include "Human.h"

class Surfer : public Human
{
	public:
	      Surfer(int x, int y);
};
#endif 


Surfer.cpp (Just For Reference)
1
2
3
4
5
6
#include "Surfer.h"

Surfer::Surfer(int x, int y) : Human('X', x, y)
{
    
}


SwimmingArea.h (Problem Here)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#ifndef SWIMMINGAREA_H
#define SWIMMINGAREA_H
#include "Surfer.h"
#include "PositionMover.h"

class SwimmingArea
{
	public:
	      SwimmingArea(int inWidth, int inHeight);
	      ~SwimmingArea();
	      
	      int getWidth() const;
	      int getHeight() const;
	      Surfer* getSurfer() const;
	      PositionMover* createPositionMover(PositionMover* m);
	private:
	      int mWidth;
	      int mHeight;
	      Surfer* mSurfer;
};
#endif 


SwimmingArea.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#include "SwimmingArea.h"

SwimmingArea::SwimmingArea(int inWidth, int inHeight)
{
    mWidth = inWidth;
    mHeight = inHeight;
    
    mSurfer = new Surfer(mWidth/2, mHeight/2);
}

SwimmingArea::~SwimmingArea()
{
    mWidth = 0;
    mHeight = 0;
    
    delete mSurfer;
}

int SwimmingArea::getWidth() const
{
    return mWidth;
}

int SwimmingArea::getHeight() const
{
    return mHeight;
}

Surfer* SwimmingArea::getSurfer() const
{
    return mSurfer;
}

PositionMover* SwimmingArea::createPositionMover(PositionMover* m)
{
    //Dynamically allocate and return an object of PositionMover
}


PositionMover.h (Problem Here)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#ifndef POSITIONMOVER_H
#define POSITIONMOVER_H
#include "SwimmingArea.h"

class PositionMover
{
	public:
	      PositionMover(){}
	      PositionMover(SwimmingArea* m);
	      
	      void left();
	      void right();
	      void up();
	      void down();
	private:
	      SwimmingArea* mSwimmingArea;
};
#endif 


PositionMover.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#ifndef POSITIONMOVER_H
#define POSITIONMOVER_H
#include "SwimmingArea.h"

class PositionMover
{
	public:
	      PositionMover(){}
	      PositionMover(SwimmingArea* m);
	      
	      //Still have to implement
	      void left();
	      void right();
	      void up();
	      void down();
	private:
	      SwimmingArea* mSwimmingArea;
};
#endif 


Any help in order to solve the referencing problem would be greatly appreciated.
Last edited on
PositionMover.h is including SwimmingArea.h
and SwimmingArea.h is including PositionMover.h

So yeah. Circular reference problem.

I explain the problems/solutions in the below linked thread. If you are interested in a bit of reading, here you go:

http://www.cplusplus.com/forum/articles/10627/#msg49679


If you just want a fix, here is my recommendation:

SwimmingArea.h:
1
2
3
4
5
6
7
8
#ifndef SWIMMINGAREA_H
#define SWIMMINGAREA_H
//#include "Surfer.h"    <- get rid of this
//#include "PositionMover.h"  <- get rid of this
class Surfer;  // <- add this
class PositionMover;  // <- add this

// ... 


SwimmingArea.cpp
1
2
3
#include "Surfer.h"    // <- add this
#include "PositionMover.h"  //  <- add this
#include "SwimmingArea.h" 


PositionMover.h
1
2
3
4
5
6
#ifndef POSITIONMOVER_H
#define POSITIONMOVER_H
//#include "SwimmingArea.h"  // <- get rid of this
class SwimmingArea;  // <- add this

//... 


PositionMover.cpp
1
2
3
4
#include "SwimmingArea.h" // <- add this
#include "PositionMover.h"

//... 
Thank you very much :)
Topic archived. No new replies allowed.