Hi everyone,
I'm trying to make a system that manage schools (with students, classes, teachers, ect). I'm having problems trying to create a vector of school classes
#include<iostream>
usingnamespace std;
class Test {
public:
void check(class Other o); // foward declaration of class Other
};
class Other { // class definition
private:
int i;
public:
Other(int j) : i(j) {}
int geti() const { return i; }
};
void Test::check(class Other o) { // method definition
cout << o.geti() << "\n";
}
int main() {
class Test t; // "class" is not needed here
class Other o(3); // or here
t.check(o);
return 0;
}
Forward declaration for classes are normally written like this:
#include<iostream>
usingnamespace std;
class Other; // foward declaration of class Other
class Test {
public:
void check(Other o);
};
class Other { // class definition
private:
int i;
public:
Other(int j) : i(j) {}
int geti() const { return i; }
};
void Test::check(class Other o) { // method definition
cout << o.geti() << "\n";
}
int main() {
Test t;
Other o(3);
t.check(o);
return 0;
}
I can see you have circular link dependencies - school.h includes Include.h, and Include.h includes school.h. That may be the cause of the problem. In any case, it indicates that you haven't thought clearly about the design.
In general, trying to include every header file in every translation unit is a bad idea. You want to keep your dependencies as tidy and stripped-down as possible. If A.h needs to include B.h, it should include only B.h, not every other header in your project.
#pragma once (or the older-style include guards) solve a specific problem. They don't magically vanish away all problems caused by circular dependencies.