0

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.

1 Answer 1

4

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?

Sign up to request clarification or add additional context in comments.

3 Comments

Not sure.. just playing around.. I have a finite set of data layout type for openGL's VBO, so I might be the case.
Well, have fun fiddling around. The problem with doing that is that you really must make sure of it, and in general that's just too cumbersome.
@Decuplicator: I probably don't want to let client classes specialize the template class with arbitrary types. It's a template for now just to let me save some code. Anyway.. I am just playing around :) .. thanks for the answer, you helped me with my silly mistake ..

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.