初始化列表向量构造函数

initializer list vector constructor

我正在尝试定义一个 class 构造函数,它采用 initializer_list 参数并使用它来构造包含的向量。

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

编译失败,出现以下错误:

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

不确定我做错了什么。有什么提示吗?

我正在 Windows 使用 QTCreator 5.4.2 和 MinGW 进行编译。

您正在尝试在完全创建的 vector 上调用 call-operator (operator())。
您应该使用 ctor-init-list 中的构造函数(首选),或者调用 member-function assign.

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

顺便说一句,您真的确定定义您的模板成员implementation-file会起作用吗?
您真的在那里实例化了所有需要的特化吗?