簡體   English   中英

std :: vector不接受我的struct作為模板

[英]std::vector doesnt accept my struct as template

所以我嘗試創建一個包含我的struct tabsstd::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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM