我試圖專注模板類成員函數:模板專業化
在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]
有什麼不對?
@πάνταῥεῖ顯式特化的實現可以放在'cpp'文件中。 – Constructor
@Constructor但是,然後他們需要出現在正確的命名空間,但。 –
@πάνταῥεῖ當然你是對的。 – Constructor