2016-09-24 59 views
0
#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故障使用模板: '类' 型redifinition

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 

我不明白,这个错误的根源错误,如果我对这个问题变得更加开明了,我会将问题的重新格式化,以便更清晰

+0

代码中的“foo”和“bar”是什么? – xinaiz

+0

班级名称(Foo)和枚举名称(bar)的站点名称 – brettmichaelgreen

+0

标题中的“第44集”是什么意思? – tambre

回答

0

如果你想要有dif ferent 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};  
}; 

这样你就不必重新定义所有的类成员只是因为你想不同的枚举专业化。