我的学校项目遇到了一些麻烦。
我有一堂课:
#include "Group.h"
#include <vector>
#include <string>
using namespace std;
class User{
private :
string username;
vector<Group*> groups;
void show() {
for(int i=0; i<groups.size(); i++)
cout << groups[i]->getName() << "\n";
}
string getUsername(){return username;}
};
和
#include "User.h"
#include <vector>
#include <string>
using namespace std;
class Group{
private :
string name;
string getName(){return name;};
User *f;
vector<User*> m;
void show(){
for(int i=0; i<m.size(); i++)
cout << m[i]->getUsername() << "\n";
}
};
当我尝试编译它时,它给了我错误:
E:\Group.h|31|error: ISO C++ forbids declaration of 'User' with no type| E:\Group.h|31|error: expected ';' before '*' token|
E:\Group.h|33|error: 'User' was not declared in this scope|
E:\Group.h|33|error: template argument 1 is invalid|
E:\Group.h|33|error: template argument 2 is invalid|
E:\Group.h|36|error: 'User' was not declared in this scope|
E:\Group.h|36|error: template argument 1 is invalid|
E:\Group.h|36|error: template argument 2 is invalid|
E:\Group.h|47|error: 'User' has not been declared|
E:\Group.h|47|error: 'User' was not declared in this scope|
E:\Group.h|47|error: template argument 1 is invalid|
E:\Group.h|47|error: template argument 2 is invalid|
E:\Group.h|58|error: ISO C++ forbids declaration of 'User' with no type| E:\Group.h|58|error: expected ';' before '*' token|
E:\Group.h|59|error: 'User' has not been declared|
E:\Group.h|60|error: 'User' was not declared in this scope|
E:\Group.h|60|error: template argument 1 is invalid|
E:\Group.h|60|error: template argument 2 is invalid|
E:\Group.h|61|error: 'User' was not declared in this scope|
E:\Group.h|61|error: template argument 1 is invalid|
E:\Group.h|61|error: template argument 2 is invalid| ||=== Build finished: 21 errors, 4 warnings ===|
怎么了?
只有当我添加class User;
到 Group.h 文件和class Group;
User.h 文件时它才会编译,但这不是我寻找正确解决方案的重点,而不仅仅是临时解决方案。