模板问题:"类"类型编辑
Trouble with Templates: 'class' type redifinition
#pragma once
//includes
template<class RefType>
class Foo
{
public:
template<>
enum Foo<QString>::bar { //values A }; //LINE X
template<>
enum Foo<double>::bar { //values B };
template<>
enum Foo<Kraken::Point3>::bar { //values C };
//functions
}; //LINE Y
编译器给出LINE X
的错误error C2011: 'Foo<QString>': 'class' type redefinition
与注释
note: see declaration of 'Foo<QString>' LINE X
note: note: see reference to class template instantiation 'Foo<RefType>'LINE Y
我不明白这个错误的来源,如果我对这个问题有更多的了解,我会重新格式化问题,使其更清晰
如果你想为选择的类型有不同的enum
,你应该用这些类型专门化你的模板类:
template<class RefType>
class Foo
{
//default enum if you want
};
template<>
class Foo<QString>
{
enum bar {Q1, Q2, Q3};
};
template<>
class Foo<double>
{
enum bar {d1, d2, d3};
};
template<>
class Foo<Kraken::Point3>
{
enum bar {K1, K2, K3};
};
你的代码看起来像是你想专门化类模板的一些成员,但这在c++中是不可能的。
在保留大部分类结构的同时破解这个问题的一种方法是通用类实现的公共继承:template<class Reftype>
class FooImpl
{
RefType x;
public:
void set_x(RefType val) {x=val;}
RefType get_x(void) {return x;}
};
template<class RefType>
class Foo : public FooImpl<Reftype>
{
};
template<>
class Foo<QString> : public FooImpl<QString>
{
enum bar {Q1, Q2, Q3};
};
template<>
class Foo<double> : public FooImpl<double>
{
enum bar {d1, d2, d3};
};
template<>
class Foo<Kraken::Point3> : public FooImpl<Kraken::Point3>
{
enum bar {K1, K2, K3};
};
这样,您就不必仅仅因为想要在特化中使用不同的枚举而重新定义所有的类成员。
相关文章:
- ArduinoJson 6.15.2:JsonObject没有命名类型
- 防止主数据类型C++的隐式转换
- 大量序列中核苷酸类型的快速计数
- 如何从C++中的依赖类型中获得它所依赖的类型
- 有关插入适配器的错误。[错误]请求从 'back_insert_iterator<vector<>>' 类型转换为非标量类型
- 是否可以初始化不可复制类型的成员变量(或基类)
- 如何获取std::result_of函数的返回类型
- 从父命名空间重载类型
- 如果C++类在类方法中具有动态分配,但没有构造函数/析构函数或任何非静态成员,那么它仍然是POD类型吗
- 我想将一个对T类型的非常量左值引用绑定到一个T类型的临时值
- 我的编辑距离递归代码中的字符类型有问题
- 如何检测焦点窗口是否为编辑"类型"控件?
- 如何在编辑控件中显示来自LPVOID类型的字符串
- Eclipse 3.7无法在C++编辑器中解析类型
- 哪些 IDE 和文本编辑器可以推断出在 C++11 中使用 auto 关键字声明的变量类型
- Qt - 无效使用不完整的类型 'class QScrollBar' - 为文本编辑小部件添加水平滚动条
- 自定义构件上的 float 类型的Q_PROPERTY不会显示在属性编辑器中
- 一种单一的数据结构,用于从脚本中读取任意类型的变量,以便在运行时进行检索和编辑
- 模板问题:"类"类型编辑
- 当使用Android NDK时,Eclipse编辑器将无法识别c++模板类型