Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/161.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 模板参数1无效(代码::Blocks Win Vista)-我不';不要使用模板_C++_Codeblocks - Fatal编程技术网

C++ 模板参数1无效(代码::Blocks Win Vista)-我不';不要使用模板

C++ 模板参数1无效(代码::Blocks Win Vista)-我不';不要使用模板,c++,codeblocks,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<gro

我的学校项目有些麻烦

我有一门课:

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

};
怎么了

它只在我添加
类用户时编译
到Group.h文件,以及
类组到User.h文件,但这不是我要寻找的正确解决方案,而不仅仅是临时解决方案


我的整个项目:

标题中有循环依赖项。您可以通过将实现移动到
.cpp
文件并向前声明您使用的类来修复它:

#include <vector>
#include <string>

class Group; // forward declaration

class User{
    private :
        std::string username;
        std::vector<Group*> groups;
        void show();
};
#包括
#包括
类组;//远期申报
类用户{
私人:
std::字符串用户名;
std::向量组;
void show();
};

#包括
#包括
类用户;//远期申报
班级{
私人:
std::字符串名;
std::string getName(){return name;};
用户*f;
std::向量m;
void show();
};
然后,在实现文件中,可以包含头

还请注意,您应该避免在标题和大范围中使用
名称空间std

您使用的是模板。您使用的是
vector
,它是一个模板

您将收到所执行的错误,因为当您到达代码的以下部分时,未定义用户类:

User *f;
vector<User*> m;
User*f;
向量m;
为什么它没有定义?毕竟您包含了User.h头?是的,但是User.h头还包括Group.h头,因此必须先读取其中一个


要解决这个问题,您需要更改标题,以便其中只有一个包含另一个(或者两者都不包含另一个)。为此,您首先需要将方法定义移到C++文件中,这样就不会在头中出现其他类方法的方法调用。然后您可以向前声明另一个类并删除#include。

您有一个循环依赖项。两个文件都需要彼此编译

尝试在组中声明用户:

#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(); 
};
#包括
#包括
类用户;
班级{
私人:
std::字符串名;
std::string getName(){return name;};
用户*f;
std::向量m;
void show();
};
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";
    }

    .....
 }
#包括“Group.h”
#包括“User.h”
使用名称空间std;
班级
{
.....
无效显示(){

对于(int i=0;i
vector
和“我不使用模板”不要放在一起。这些文件分别名为user.h和group.h吗?它们相互包含吗?你在group.h中包含user.h,在user.h中包含group.h。这可能会破坏你的代码。此外,你在尝试从组中调用user的私有方法时仍会遇到困难。如果你的老师教过你这种编码风格,请在练习中忽略他以后,得到一本好的C++书。两个最明显的不去的地方:缺失包括警卫。<代码>在页眉文件中使用命名空间< /代码>。我嗅到老师也教你<代码>空洞主体()< <代码> > <代码>退出()。
而不是返回。请不要。它会给我:E:\User.cpp | 27 |错误:不完整类型“struct Group”的使用无效;E:\User.h | 16 |错误:转发“struct Group”|@mazix声明,这是因为您可能正在调用Group的方法,而该方法不会出现在您的示例中(如果您删除了实现)。只要你还在头文件中引用它的方法(如
getUsername
),你就不能(仅仅)转发declare User。是的,他必须将它移到cpp文件中。谢谢。@RC:'好的,我试着按照你说的做,但问题根本没有解决。这是我的整个项目:希望它能帮助你:)目前我无法查看您的项目,但有几件事您应该看一下。您现在不在组中包含用户,因此在更改之前,如果用户只包含组,则使用的任何文件都可以。确保它们包含所需内容并包含User.h。此外,您的包含文件需要有防护装置。@RC://确定,不要自寻烦恼,我想我已经解决了:。非常感谢你帮助我!我真的很感激:)
#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(); 
};
#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";
    }

    .....
 }