Avoiding circular dependencies

Hey all

I'm trying to write a program like so:
I have a cpp file I waould like to run and create objects of 2 classes.
Each class has a field of the other class.
I can't seem to solve this after a lot of trials and searches.
Can anyone help?

added the code:

/*
* Classes.cpp
*
* Created on: Nov 4, 2012
* Author: achie
*/
#include <iostream>
#include <string>
#include "../include/classes.h"
#include "../include/Class1.h"
#include "../include/Class2.h"


using namespace std;

int main(){
Class1 class1 (7);
cout << class1.number << endl;
}



/*
* Class1.cpp
*
* Created on: Nov 4, 2012
* Author: achie
*/

#include "../include/Class1.h"
class class2;

Class1::Class1(int a,vector<Class2> b) {
number=a;
Class2Classes=b;
}


Class1::~Class1() {
// TODO Auto-generated destructor stub
}



/*
* Class1.h
*
* Created on: Nov 4, 2012
* Author: achie
*/

#ifndef CLASS1_H_
#define CLASS1_H_
#include <iostream>
#include <string>
#include <vector>
#include "../include/Class2.h"


using namespace std;

class Class1 {
public:
Class1(int number, vector<Class2> Class2Classes);
virtual ~Class1();
int number;
vector<Class2> Class2Classes;
friend class Class2;
};


#endif /* CLASS1_H_ */



/*
* Class2.cpp
*
* Created on: Nov 4, 2012
* Author: achie
*/

#include "../include/Class2.h"
class Class1;

Class2::Class2(string a, vector <Class1> b) {
name=a;
Class1Classes=b;
}

Class2::~Class2() {
// TODO Auto-generated destructor stub
}



/*
* Class2.h
*
* Created on: Nov 4, 2012
* Author: achie
*/

#ifndef CLASS2_H_
#define CLASS2_H_

#include <iostream>
#include <string>
#include <vector>

#include "../include/Class1.h"


using namespace std;


class Class2 {
public:
Class2(string name,vector<Class1> Class1Classes);
virtual ~Class2();
string name;
vector<Class1> Class1Classes;
friend class Class1;

};

#endif /* CLASS2_H_ */


/*
* classes.h
*
* Created on: Nov 4, 2012
* Author: achie
*/

#ifndef CLASSES_H_
#define CLASSES_H_
#include "classes.h"
#include <string>

#endif /* CLASSES_H_ */



This is the error I get:

In file included from src/../include/Class1.h:13:0,
from src/Class1.cpp:8:
src/../include/../include/Class2.h:23:28: error: ‘Class1’ was not declared in this scope
src/../include/../include/Class2.h:23:34: error: template argument 1 is invalid
src/../include/../include/Class2.h:23:34: error: template argument 2 is invalid
src/../include/../include/Class2.h:26:9: error: ‘Class1’ was not declared in this scope
src/../include/../include/Class2.h:26:15: error: template argument 1 is invalid
src/../include/../include/Class2.h:26:15: error: template argument 2 is invalid
src/Class1.cpp: In constructor ‘Class1::Class1(int, std::vector<Class2>)’:
src/Class1.cpp:11:1: warning: ‘Class1::number’ should be initialized in the member initialization list
src/Class1.cpp:11:1: warning: ‘Class1::Class2Classes’ should be initialized in the member initialization list


Thank you!
#1 Could you please go back and edit your post so it uses code tags

How to use code tags
http://www.cplusplus.com/forum/articles/42672/

#2 Try editing you code (it there were lines numbers, as there would be if the code was tagged...) so the header files just forward declare the other class.

e.g.

class Class;

rather than

#include "../include/Class2.h"

in Class1.h and similarly for Class2.h

Then edit your cpp file the other way round (it is unusual to see a forward definition for a class in a cpp file rather than the header, it it exists).

Andy

PS Note that the above (probably) works as vector<> uses a pointer to your classes internally. (The probably is because I don't know the behavior of all implementations of vector)

This trick would not work if you'd used a C-style array as the compiler would then have to see the complete class declaration up front before working out how to layout memory for the variables which use your class. When it's a pointer, the compiler already knows the size of a pointer, so it works ok. It's happy to wait until you first use the pointer.
Last edited on
Topic archived. No new replies allowed.