模板参数1无效(Code :: Blocks Win Vista) - 我不使用模板

maz*_*zix 1 c++ codeblocks

我的学校项目有些麻烦.

我有一节课:

#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;}

};
Run Code Online (Sandbox Code Playgroud)

#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";
        }
};
Run Code Online (Sandbox Code Playgroud)

当我尝试编译它时,它给了我错误:

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 ===|
Run Code Online (Sandbox Code Playgroud)

怎么了?

它仅在我添加class User;到Group.h文件和class Group;User.h文件时编译,但它不是我正在寻找正确的解决方案,而不仅仅是临时解决方案.

我的整个项目: http ://www.speedyshare.com/jXYuM/proj.tar

RC.*_*RC. 12

你有一个周期性的依赖.两个文件都需要彼此编译.

尝试在组中向前声明用户:

#include <vector>
#include <string>

class User;

class Group{
    private :
        std::string name;
        std::string getName(){return name;};
        User *f;
        std::vector<User*> m;
        void show(); 
};
Run Code Online (Sandbox Code Playgroud)

Group.cpp

#include "Group.h"
#include "User.h"

using namespace std;

class Group
{
    .....

    void show() {
        for(int i=0; i<m.size(); i++)
        cout << m[i]->getUsername() << "\n";
    }

    .....
 }
Run Code Online (Sandbox Code Playgroud)

然后在Group.cpp文件中包含User.

只要对象大小不依赖于前向声明的对象的实际大小,就可以在标题中转发声明.在这种情况下,(在Group中)使用User作为指针,因此Group的大小不依赖于User的大小,它只存储一个独立于User大小的指针.

应该有帮助的另一个小问题是在头文件中包含名称空间(在你的情况下是std)是不好的做法.你应该删除"using"语句并改为使用std :: vector.在cpp文件中使用"using"是可以的,因为其他代码不会"包含"您的源代码.