2014-05-18 41 views
0

我试图专注模板类成员函数:模板专业化

在valueinput.h

namespace Gui 
    { 
    template<class T> 
    class ValueInput:public TextInput 
     { 
     public:   
      static ValueInput* create(Gui& gui_obj,uint32_t style_0,uint32_t style_1 
       ,Window* parent,T& obj) 
       {return new ValueInput(gui_obj,style_0,style_1,parent,obj);} 

      //Polymorphic implementation inherited from 
      //TextInput that needs specialization depending on T 
      void valueUpdate(); 

      //Polymorphic implementation inherited from 
      //TextInput that needs specialization depending on T 
      void displayUpdate(); 

     protected: 
      ValueInput(Gui& gui_obj,uint32_t style_0,uint32_t style_1,Window* parent 
       ,T& obj):TextInput(gui_obj,style_0,style_1,parent),ptr_obj(&obj) 
       {} 

     private: 
      T* ptr_obj; 
     }; 
    } 

在valueinput.cpp

template<> 
void Gui::ValueInput<double>::displayUpdate() 
    { 
    Dialog::messageDisplay(this,{STR("Display Update"),Herbs::LogMessage::Type::INFORMATION},STR("Test")); 
    } 

template<> 
void Gui::ValueInput<double>::valueUpdate() 
    { 
    Dialog::messageDisplay(this,{STR("Value Update"),Herbs::LogMessage::Type::INFORMATION},STR("Test")); 
    } 

编译器输出:

g++ "valueinput.cpp" -g -municode -Wall -c -std=c++11 -o "__wand_targets_dbg\valueinput.o" 

valueinput.cpp:21:45: error: specialization of 'void Gui::ValueInput::displayUpdate() [with T = double]' in different namespace [-fpermissive]

valueinput.cpp:21:6: error: from definition of 'void Gui::ValueInput::displayUpdate() [with T = double]' [-fpermissive]

valueinput.cpp:27:43: error: specialization of 'void Gui::ValueInput::valueUpdate() [with T = double]' in different namespace [-fpermissive]

valueinput.cpp:27:6: error: from definition of 'void Gui::ValueInput::valueUpdate() [with T = double]' [-fpermissive]

有什么不对?

+0

@πάνταῥεῖ显式特化的实现可以放在'cpp'文件中。 – Constructor

+0

@Constructor但是,然后他们需要出现在正确的命名空间,但。 –

+0

@πάνταῥεῖ当然你是对的。 – Constructor

回答

1

重写代码在你valueinput.cpp文件以下列方式:

namespace Gui 
{ 
    template<> 
    void ValueInput<double>::displayUpdate() 
    { 
     Dialog::messageDisplay(this,{STR("Display Update"),Herbs::LogMessage::Type::INFORMATION},STR("Test")); 
    } 

    template<> 
    void ValueInput<double>::valueUpdate() 
    { 
     Dialog::messageDisplay(this,{STR("Value Update"),Herbs::LogMessage::Type::INFORMATION},STR("Test")); 
    } 
} 

如果你想使用它们valueinput.cpp文件之外,不要忘记在valueinput.h头文件来声明这些专业:

namespace Gui 
{ 
    template<class T> 
    class ValueInput : public TextInput 
    { 
     // ... 
    }; 

    template<> 
    void ValueInput<double>::displayUpdate(); 

    template<> 
    void ValueInput<double>::valueUpdate(); 

} 

编辑:我不知道你的变体是否符合标准。但这里是从标准小报价([temp.expl.spec] 14.7.3/8):

A template explicit specialization is in the scope of the namespace in which the template was defined. [ Example:

namespace N { 
    template<class T> class X { /* ... */ }; 
    template<class T> class Y { /* ... */ }; 

    template<> class X<int> { /* ... */ };  // OK: specialization 
               // in same namespace 
    template<> class Y<double>;     // forward declare intent to 
               // specialize for double 
} 

template<> class N::Y<double> { /* ... */ }; // OK: specialization 
               // in same namespace 

— end example ]

不幸的是它是关于类模板特,不是关于功能模板特或类的功能部件特模板。

+0

所以编译器无法理解Gui在这种情况下是一个命名空间。为什么? – user877329

+0

@ user877329我不知道。但*铿锵3.4 * [编译此类代码](http://rextester.com/MGIAX36625)。可能它是* gcc *中的一个错误。 – Constructor

+0

@ user877329请参阅我的除了您可能感兴趣的标准引用的答案。 – Constructor