Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/149.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/google-apps-script/5.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++ std::vector不接受我的结构作为模板_C++ - Fatal编程技术网

C++ std::vector不接受我的结构作为模板

C++ std::vector不接受我的结构作为模板,c++,C++,因此,我尝试制作一个包含我的结构选项卡的std::vector 菜单 class CMenu { public: void CMenu::addtab(std::string label, CCords pos, int index); private: std::vector<tabs> tablist; }; struct tabs { std::string label; SPoint pos; int index; }; 当我试图编

因此,我尝试制作一个包含我的结构选项卡的
std::vector

菜单

class CMenu
{
public:
    void CMenu::addtab(std::string label, CCords pos, int index);
private:
    std::vector<tabs> tablist;
};

struct tabs
{
    std::string label;
    SPoint pos;
    int index;
};
当我试图编译它时,我会得到这些错误

1>c:\cpp\testmenu\menu.h(57): error C2065: 'tabs': undeclared identifier
1>c:\cpp\testmenu\menu.h(57): error C2923: 'std::vector': 'tabs' is not a valid template type argument for parameter '_Ty'
1>c:\cpp\testmenu\menu.h(57): error C3203: 'allocator': unspecialized class template can't be used as a template argument for template parameter '_Alloc', expected a real type

正式地说,
tabs
std::vector
看到它时需要是一个完整的类型。因此,即使向前声明
选项卡
(表示不完整的类型)也是不够的

这意味着
struct
定义必须出现在
CMenu
之前

注意,这个规则在C++17中稍微放宽了一点,在C++17中,向量的声明和实例化的类型可能不完整,但要受到一些围绕向量分配器的约束;本标准的相关部分:

[vector.overview]/3当 如果分配器满足分配器,则实例化向量 完整性要求17.6.3.5.1。T应在任何 向量的结果专门化的成员被引用


Irc,不完整类型支持<代码> STD::vector < /C> >,<代码> STD::C++中的列表< /代码>17@Jarod42这仅适用于模板实例化。对于
push_back
和许多其他操作,您仍然需要一个完整的类型。@Jarod42:For
std::vector
您肯定是正确的。我已经添加了。@Bathsheba我在用户的问题中没有看到任何转发声明(尽管他的示例也不是mcve)。它可能很简单,比如忘记包含相关的标题。不确定他们是否熟悉转发声明,更不用说不完整的类型、分配器和向量模板实例化了。@DanM:我也不知道。但是,这个问题问得很好(这就是我回答它的原因)。我花时间解释了完整类型和不完整类型之间的区别,这与C++17特别相关。请记住,答案不一定适合OP。
1>c:\cpp\testmenu\menu.h(57): error C2065: 'tabs': undeclared identifier
1>c:\cpp\testmenu\menu.h(57): error C2923: 'std::vector': 'tabs' is not a valid template type argument for parameter '_Ty'
1>c:\cpp\testmenu\menu.h(57): error C3203: 'allocator': unspecialized class template can't be used as a template argument for template parameter '_Alloc', expected a real type