Mutual Class problem - A receives B object in method and B uses A as parameter

Hi!
I am having problems with two Classes. In Class Position one method received the instance of Class Swarm.
But in Class Swarm I use the Class Position as part of its private atributes.

This gives me a linker error saying I need a library to link to, but there is no library.

I've been trying to use forward declaration to call the Swarm object from the positionUpdate method but I get errors.
Additionally, I can't access the Swarm methods to get or set its properties.

The position header

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
39
40
41
42
43
44
45
46
47
48
49
50
51
#ifndef POSITION_H
#define POSITION_H

#include "listofincludes.h"
#include "bits.h"
#include "nums.h"
#include "problem.h"
#include "parameter.h"

class swarm;

class position {
private:
	bits bit;
	nums num;
	double f;
	double evalNb;
	vector<bits>::const_iterator nPermutIterator;

public:
	position();
	~position();
	
	bits getFromPosition (const vector<bits> & vecBit, const int & val);
	
	position positionUpdate (const int & s0, 
const problem & nProblem, 
const parameter & nParam, 
swarm & nSwarm, 
const vector<bits> & nPermut);

	void positionDisplay();
	position positionInit(	const problem & nProblem, 
const parameter & nParam,	
const vector<bits> & nPermut);
	double	distance(	const position & x2);
	double performance (const problem & problem);
	bits getBit() const;
	nums getNum() const;
	double getF() const;
	double getEvalNb() const;
	
	void setBit(const bits & val);
	void setNum(const nums & val);
	void setF(const double & val);
	void setEvalNb(const double & val);
	
};

#endif



The Swarm Header

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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#ifndef SWARM_H
#define SWARM_H

#include "listofincludes.h"
#include "velocity.h"
#include "position.h"

class swarm
{
public:
	swarm();
	~swarm();

	int getSize() const;
	position getElementVectorX(const int & val); 
	velocity getElementVectorV(const int & val);
	position getElementVectorXBest(const int & val); 
	position getBest() const;
	int getLinks(const int & coefficient01,	
const int & coefficient02) const;
	int getStrategy(const int & coefficient) const;

	
void setElementVectorX(const position & val, 
const int & coefficient);
	
void setElementVectorV(const velocity & val, 
const int & coefficient);
	
void setElementVectorXBest(const position & val,
 const int & efficient);

	void setBest(const position & val);
	
void setLinks(const int & val,
const int & coefficient01, const int & coefficient02);
	
void setStrategy(const int & val, 
const int & coefficient);

private:
	int size;
	vector<position> x;
	vector<position>::const_iterator xIterator;
	vector<position>::iterator setXIterator;
	vector<velocity> v;
	vector<velocity>::const_iterator vIterator;
	vector<velocity>::iterator setVIterator;
	vector<position> xBest;				//Previous best
	vector<position>::const_iterator xBestIterator;
	vector<position>::iterator setXBestIterator;
	position best;								// Best best
	int	links[sMax][sMax];			// Information links
	int	strategy[sMax];					// 
};

#endif 
Last edited on
http://www.cplusplus.com/forum/articles/10627/
Should have how to solve your problem. ;)
Not quite.
Option 1: Forward declaring and inlining the function inside the Header causes an error C2027: use of undefined type 'swarm'.
Option 2: Not inlining the function and putting it in the cpp but forward declare it (as it was before) causes a linking problem (same, can't find the functions).

Option 3: As it is now, in another header file as an inline function that includes the header from the forward declaring class. Gives error C2027: use of undefined type 'swarm'

Both Option 1 and 3 came from your link

Last edited on
Can you please post the exact error output?

So in Position you forward declare swarm, then in an implementation file (not the bottom of the header file) you #include "swarm.h" and do your updatePosition implementation there?

In the swarm .cpp you have everything implemented for class swarm?

In other words, you have fully implemented these classes somewhere?

Option 2: Not inlining the function and putting it in the cpp but forward declare it (as it was before) causes a linking problem (same, can't find the functions).
You did #include in the cpp for the forward declared class right?
Last edited on
Topic archived. No new replies allowed.