My searches have lead me to believe that the problem I am experiencing is called cyclic redundancy. I don't understand any of the solutions that are posted. I am (fairly) new to C++, coming from a strong Java background.
Basically there are two classes that depend on each other. Class A contains a vector of class B objects, and class B contains methods that require class A objects as input.
Here's code that reproduces the problem.
According to codelite g++, the error is in school.h and is "person was not declared in this scope". It also says "template argument 1 is invalid" and "template argument number 2 is invalid". Then a couple of others, about non-class type "int" on all the vector functions that get invoked.
main.cpp
#include <iostream>
#include <string>
#include "person.h"
#include "school.h"
int main() {
person p;
school s;
std::cout << p.name << std::endl;
s.personVect.push_back(p);
std::cout << s.personVect.size() << std::endl;
std::cout << s.personVect.at(0).name << std::endl;
p.test();
return 0;
}
school.h
#ifndef SCHOOL_H
#define SCHOOL_H
#include <vector>
#include "person.h"
class school
{
public:
school();
~school();
std::vector<person> personVect;
};
#endif // SCHOOL_H
school.cpp
#include "school.h"
school::school(){}
school::~school(){}
person.h
#ifndef PERSON_H
#define PERSON_H
#include <string>
#include <vector>
#include "school.h"
class person {
public:
std::string name;
std::string phone;
school doSomethingWithSchool();
void test();
person();
~person();
};
#endif // PERSON_H
person.cpp
#include "person.h"
#include <iostream>
using namespace std;
person::person()
{
name = "marcus";
phone = "0400000000";
}
person::~person()
{
}
void person::test() {
cout << this->name;
}
school person::doSomethingWithSchool() {
school s;
}