How to avoid circulae 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!
Circular dependencies indicate a design flaw. If two classes require each other to be built, then a misunderstanding of what a class is supposed to do has occurred.
Topic archived. No new replies allowed.