可能重複:
Why should the implementation and the declaration of a template class be in the same header file?派生類中定義的頭文件和定義模板,以CPP文件
我試圖定義一個頭文件模板,定義它在一個cpp文件中,模板應該在派生類中定義。所以這是我得到:
頭文件:
#ifndef ........
#define .....
template <class mytypename>
class abcBaseClass:public abcDerivedClass{
public:
mytypename getvalue(char*)
};
#endif
源文件:
mytypename abcDerivedClass<mytypename>::getvalue(char* name){
}
我只是想知道這是做的正確方法它?。
這就是我想實現......我想打這個電話是
double x = a->getvalue<double>(char)
實現必須以與聲明相同的文件結束,除非您顯式實例化類型。 'typename'也是一個保留字。 –
正確的方法是將整個模板定義放在標題中。 –
你真的想做什麼? 'typename'不是模板參數的有效名稱,在那個代碼中,'base'繼承自'derived',這至少是一個反直覺的名稱選擇。 –