简体   繁体   中英

std::vector doesnt accept my struct as template

So I try to make a std::vector containing my struct tabs

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);
}

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. So even a forward declaration of tabs (which would denote an incomplete type ) is not sufficient.

That means that the struct definition has to appear before 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; 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. T shall be complete before any member of the resulting specialization of vector is referenced.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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