c 模板類如何定義模板成員函式

2021-05-28 11:06:38 字數 994 閱讀 3611

最近跟網上乙個解決乙個模板問題

其實他在用g++編譯器寫模板定義成員函式,我在vs 05的c++編譯器上寫,然後我測試模板的結果是:

特例模板不可以在類外定義,不管是在標頭檔案還是cpp檔案都要報錯,然後跟他說讓他寫在類中(當時不知道他在用g++編譯器),

於是悲劇的問題出來了……他那個老是報沒在未命名空間類域中這樣的錯誤。

後來,還是他去問了同學…………其實只要在類中申明模板函式特例,在.tem檔案中實現,而且不用在函式後加特例型別,

void   testclassfunc()

哎,最後我試了一下用這樣的方式在vs05下是不能編譯通過的。所以在vs05編譯器不能使用那樣的方法,其實一般寫模板類直接都整在乙個模板類中實現就行了……雖然臃腫,但是不會出錯,還是挺好的…………

最後再放上一段ibm那邊的示例**…………話說看了之後也沒什麼感覺…………

#include using namespace std;

templateclass x ;

};templatetemplatetemplatevoid x::y::f(u, v)

templatetemplatevoid x::y::g(u)

template<> template<>

void x::y::g(int)

template<> template<> templatevoid x::y::f(int, v)

template<> template<> template<>

void x::y::f(int, int)

// template<> templatetemplate// void x::y::f(u, v)

// templatetemplate<>

// void x::y::g(float)

int main()

C 類模板的成員函式模板寫法

這幾天本來想將lua tinker移植到linux上去的,但是由於vc中的模板寫法與gcc中的模板寫法有些不同之處,比如下面一段 struct pop template static char invoke lua state l,int index template static const ch...

模板類中定義模板函式

用模板類和模板函式是c 程式猿必須掌握的技能。然而要充分運用編譯器的推導能力則不簡單。需求 建立乙個 類,每一列的型別可以任意指定 編譯期指定 每一行的元素可以任意指定 執行期確定 列數可變 編譯期指定 行數可變 執行期確定 這裡特意強調了編譯期和執行期,就是為了最大程度利用編譯器的推導能力,進行編...

類模板的成員函式

template class test private t age templatetest test templatetest test t value templatetest setage t agevalue age value templatet test getage templatet...