[英]std::vector doesnt accept my struct as template
So I try to make a std::vector
containing my struct tabs
所以我尝试创建一个包含我的struct
tabs
的std::vector
menu.h menu.h
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;
};
menu.cpp menu.cpp
void CMenu::Do()
{
this->addtab("sample tab", CCords( 100, 100 ), 0);
}
void CMenu::addtab(std::string label, CCords pos, int index)
{
tabs tab;
tab.label = label;
tab.pos = pos;
tab.index = index;
tablist.push_back(tab);
}
When i try to compile this i get these errors. 当我尝试编译这个我得到这些错误。
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
Formally speaking, tabs
needs to be a complete type when std::vector
sees it. 从形式上讲,当
std::vector
看到它时, tabs
需要是一个完整的类型 。 So even a forward declaration of tabs
(which would denote an incomplete type ) is not sufficient. 因此,即使是
tabs
的前向声明 (表示不完整的类型 )也是不够的。
That means that the struct
definition has to appear before CMenu
. 这意味着
struct
定义必须出现在CMenu
之前。
Note that this rule is relaxed a little from C++17 where the type can be incomplete for the declaration and instantiation of the vector subject to some constraints centred around the vector's allocator; 请注意,这个规则从C ++ 17中略微放宽,其中类型可能不完整,因为向量的声明和实例化受到一些以向量的分配器为中心的约束的影响; the relevant part of the standard:
标准的相关部分:
[vector.overview]/3 An incomplete type T may be used when instantiating vector if the allocator satisfies the allocator completeness requirements 17.6.3.5.1.
[vector.overview] / 3如果分配器满足分配器完整性要求17.6.3.5.1,则在实例化向量时可以使用不完整类型T. T shall be complete before any member of the resulting specialization of vector is referenced.
在引用向量特化的任何成员之前,T应该是完整的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.