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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11343288/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 15:02:52  来源:igfitidea点击:

template argument 1 is invalid (Code::Blocks Win Vista) - i don't use templates

c++codeblocks

提问by mazix

I have some troubles with my school project.

我的学校项目有一些问题。

I have a class:

我有一堂课:

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

};

and

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

When I try compile it, it gives me errors:

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

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 ===|

what's wrong?

怎么了?

It compiles only if I add class User;to Group.h file, and class Group;to User.h file but its not the point I'm looking for the right solution, not only the temporary one.

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

MY WHOLE PROJECT:http://www.speedyshare.com/jXYuM/proj.tar

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

回答by RC.

You have a cyclical dependency. Both files need one another to compile.

你有一个循环依赖。两个文件都需要相互编译。

Try forward-declaring User in group:

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

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

Group.cpp

组.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";
    }

    .....
 }

Then in the Group.cpp file, include User.

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

You can forward declare in the header anytime the object size doesn't depend on the actual size of the object your forward declaring. In this case, (in Group) you use User as a pointer and therefore the size of Group is not dependent on the size of User it's only storing a pointer which is independent of the User size.

您可以随时在标题中转发声明对象大小不取决于您转发声明的对象的实际大小。在这种情况下,(在 Group 中)您使用 User 作为指针,因此 Group 的大小不依赖于 User 的大小,它只存储一个与 User 大小无关的指针。

Another tidbit which should help is that it's bad practice to include namespaces (in your case std) in a header file. You should remove the "using" statement and do std::vector instead. Using a "using" in the cpp file is fine, as other code doesn't "include" your source.

另一个应该有帮助的花絮是在头文件中包含名称空间(在您的情况下为 std)是不好的做法。您应该删除“using”语句并改为执行 std::vector 。在 cpp 文件中使用“使用”很好,因为其他代码不“包含”您的源代码。

回答by juanchopanza

You have a cyclic dependency in the headers. You could fix it by moving the implementations to .cppfiles, and forward declaring the classes you use:

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

#include <vector>
#include <string>

class Group; // forward declaration

class User{
    private :
        std::string username;
        std::vector<Group*> groups;
        void show();
};

and

#include <vector>
#include <string>

class User; // forward declaration

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

Then, in your implementation files, you can include the headers.

然后,在您的实现文件中,您可以包含标头。

Also note that you should avoid using using namespace stdin headers and in large scopes.

另请注意,您应该避免using namespace std在标题和大范围内使用。

回答by sepp2k

You areusing templates. You're using vector, which is a template.

正在使用模板。您正在使用vector,这是一个模板。

You're getting the error you do because the User class is not defined when you reach the following part of the code:

您收到错误是因为当您到达代码的以下部分时未定义 User 类:

User *f;
vector<User*> m;

Why is it not defined - after all you're including the User.h header? Yes, but the User.h header also includes the Group.h header, so one of the two will have to be read before the other.

为什么它没有定义——毕竟你包含了 User.h 头文件?是的,但是 User.h 标头还包括 Group.h 标头,因此必须先读取两个标头之一。

To fix this you need to change your headers, so that only one of them includes the other (or they both don't include the other). To do this you first need to move your method definitions into a c++ file, so that no method calls to the other class's method appear in the header. Then you can forward-declare the other class and remove the #include.

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