简体   繁体   English

无法在C ++中制作结构向量

[英]Cannot make a vector of structs in C++

I'm trying to do the following: 我正在尝试执行以下操作:

struct Code
{
    GF2X generator;
    vector<GF2X> codeWords;
};

vector<Code> allCodes;

However, I end up with this error: 但是,我最终遇到此错误:

error: template argument for 'template<class _Alloc> class std::allocator' uses local type 'main()::Code'|

I'm completely lost as to what this means. 我完全不知道这意味着什么。 This is also my first time programming in C++. 这也是我第一次用C ++编程。

In C++03 local classes (classes defined inside a function) cannot be used as template arguments. 在C ++ 03中,不能将局部类(在函数内部定义的类)用作模板参数。 That restriction was lifted in C++11, but if your compiler does not have support for this feature you can always move the type definition outside of the function at namespace level. 在C ++ 11中取消了该限制,但是如果编译器不支持此功能,则可以始终在名称空间级别将类型定义移到函数之外。

int main()之外(但之前)定义结构。

You haven't posted all your code, but I'm guessing that you put struct Code ... inside your main function. 您尚未发布所有代码,但我猜您已将struct Code ...放入了主要函数中。 Try moving it above the start of main. 尝试将其移至main的开头上方。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM