在 C++11 中使用模板在类内创建不同长度的数组

Using a template to create arrays of varying length inside of class in C++11

本文关键字:数组 创建 C++11      更新时间:2023-10-16

假设我有一个类:

template <int S1>
class Cont {
    private:
       std::array<int, S1> nums;
    public:
       Cont(std::array<int, S1> ns);
}

尝试链接我的程序时,我收到一个错误,例如,Cont(std::array ns)未定义。那么我做错了什么吗?还是由于数组的工作方式,这永远不会起作用?

编辑:抱歉,我应该提到构造函数已定义。我给出标题只是为了说明我的意思。

Cont(std::array<int, S1> ns)

只是一个声明,你也需要定义它。

喜欢:

Cont(std::array<int, S1> ns){
    // code
}