简体   繁体   中英

initializer list vector constructor

I am trying to define a class constructor which takes and initializer_list parameter and use it to construct the contained vector.

//header
template<typename VertexType, typename IndexType>
class Mesh
{
public:
    Mesh(std::initializer_list<VertexType> vertices);
private:
    std::vector<VertexType> mVertexData;
};

// cpp
template<typename VertexType, typename IndexType>
Mesh<VertexType, IndexType>::Mesh(std::initializer_list<VertexType> vertices)
{
    mVertexData(vertices);
}

The compilation fails with the following error:

error: no match for call to '(std::vector<Vertex,
std::allocator<Vertex> >) (std::initializer_list<NRK::Vertex>&)'
mVertexData(vertices);

Not sure what I am doing wrong. Any hint?

I am compiling on Windows using QTCreator 5.4.2 and MinGW.

You are trying to call the call-operator ( operator() ) on a fully created vector .
You should either use the constructor in the ctor-init-list (preferred), or call the member-function assign .

template<typename VertexType, typename IndexType>
Mesh<VertexType, IndexType>::Mesh(std::initializer_list<VertexType> vertices)
: mVertexData(vertices)
{}

As an aside, are you really sure defining members of your template in that implementation-file will work?
You really instantiate all needed specializations there?

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