无法使用模板参数定义变量类型和大小的列表
Unable to define a list of variable type and size using template arguments
我有一个列表作为一个类的私有成员,该类有两个模板参数:type
表示列表元素的数据类型,size
表示列表中元素的数量。为此,我想使用我的两个模板参数来使用列表的填充构造函数。这是我的尝试:
#include <list>
template <typename type, unsigned int size>
class my_class {
private:
std::list<type> my_queue(size, 0);
// More code here...
};
我的方法似乎遵循了这里提供的信息和例子;但当我编译这个时,我会得到以下错误。
error: 'size' is not a type
error: expected identifier before numeric constant
error: expected ',' or '...' before numeric constant
它似乎通过默认构造函数而不是填充构造函数来识别列表的声明。有人能帮我解决这个问题吗?
谢谢!
编辑:这是我的修订来源,提供了更多详细信息。我现在在公共方法上遇到了麻烦注意:这是我类的头文件。
#include <list>
template <typename T, unsigned int N>
class my_class {
private:
std::list<T> my_queue;
public:
// Constructor
my_class() : my_queue(N, 0) { }
// Method
T some_function(T some_input);
// The source for this function exists in another file.
};
编辑2:最终实现。。。谢谢你,@billz!
#include <list>
template <typename T, unsigned int N>
class my_class {
private:
std::list<T> my_queue;
public:
// Constructor
my_class() : my_queue(N, 0) { }
// Method
T some_function(T some_input){
// Code here, which accesses my_queue
}
};
在C++11之前只能在构造函数中初始化成员变量,最好使用大写字符作为模板参数:
template <typename T, unsigned int N>
class my_class {
public:
my_class() : my_queue(N, 0) { }
private:
std::list<T> my_queue;
// More code here...
};
编辑:
T some_function(T some_input);C++只支持包含模块,您需要在声明my_class的同一文件中定义some_function
。
相关文章:
- 使用简单类型列表实现的指数编译时间.为什么
- 从类型列表中递归删除重复项会导致编译器堆空间错误 (VS2017)
- 测试两个类型列表中的所有组合
- 谷歌测试:模板模板的笛卡尔乘积的类型列表与模板
- 在迭代模板类型列表时无法停止递归
- 将可变参数类型列表的扩展打包为复杂类型的初始值设定项列表 - 合法吗?
- 阅读从 istream 到矢量的不同类型列表
- 如何在C++中创建类型列表的 n 路笛卡尔积?
- c++17通过生成预先声明的类型列表的笛卡尔乘积来生成std::变体
- 将变量模板限制为类型列表
- 我可以使用特征指定变体的类型列表吗?
- 如何对要用于模板参数的类型列表进行编码
- 创建类型列表并访问每种类型的静态成员?
- 为什么BOOST :: HANA :: EXAREMIS ::键入实验功能?类型列表的棘手是什么
- 如果创建支持返回可变参数类型列表的通用模板 API,我应该使用 std::tuple 还是其他东西?
- std::仅移动类型列表:不能在 VC++ 中放入 std::vector
- C++类型列表创建子列表
- 调用STD ::功能,并带有变体类型列表
- 在没有宏的情况下在 C++98 中创建类型列表时遇到问题
- 如何从类型列表重建参数包