![](/img/trans.png)
[英]Trying to receive a template in a struct and use it on a std::vector
[英]std::vector doesnt accept my struct as template
所以我尝试创建一个包含我的struct tabs
的std::vector
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
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);
}
当我尝试编译这个我得到这些错误。
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
从形式上讲,当std::vector
看到它时, tabs
需要是一个完整的类型 。 因此,即使是tabs
的前向声明 (表示不完整的类型 )也是不够的。
这意味着struct
定义必须出现在CMenu
之前。
请注意,这个规则从C ++ 17中略微放宽,其中类型可能不完整,因为向量的声明和实例化受到一些以向量的分配器为中心的约束的影响; 标准的相关部分:
[vector.overview] / 3如果分配器满足分配器完整性要求17.6.3.5.1,则在实例化向量时可以使用不完整类型T. 在引用向量特化的任何成员之前,T应该是完整的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.