简体   繁体   中英

c++ Push_back type with multiple

I have the following C++ Code...

struct myType
{
    int SID;
    int IID;
    std::string myString;
};

std::vector<myType[2]> _type;

The problem is when i do this...

for (int x = 1; x < 82432; x++)
{
    _type.push_back(myType());
}

it give me the following error

Error   441 error C2664: 'void std::vector<myType [2],std::allocator<_Ty>>::push_back(const myType (&)[2])' : cannot convert argument 1 from 'myType' to 'myType (&&)[2]'

The idea was to create a vector array like so: _type[82431][2].SID <<= type

Any help appreciated!

You cannot create vector of array type, one of the reason is that you cannot assign one array to other, the closes you can get is:

std::vector<std::array<myType, 2>> _type;

This is also not right

for (int x = 1; x < 82432; x++)
{
    _type.push_back(myType());
}

even if you could create vector of arrays, in your loop you are pushing data of type myType while your vector is of type myType[2] . So to make it work with std::array change to _type.push_back({myType(), myType()});

see here http://coliru.stacked-crooked.com/a/b77015b21524de77 for some other uses

由于某些愚蠢的原因...我可以做到这一点!

myType _type[82432][2];

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