我看到相關的模板錯誤(編譯器的Visual Studio 2012),我不理解之外。下面的代碼,歸結爲要領:定義明確的專業類的成員函數的類定義
// Templated class - generic
template <typename T>
class Test
{
public:
void WorksFine() {} // Comiples and works as expected at runtime
void Problem();
};
// Templated class - expicit specialization for T = int.
template <>
class Test<int>
{
public:
void WorksFine() {} // Comiples and works as expected at runtime
void Problem();
};
// The definition below compiles and works fine at runtime.
template<typename T> void Test<T>::Problem() {}
// The definition below gives error C2910.
template<> void Test<int>::Problem() {printf("In Test::Problem(int instantiation)\n");}
對於WorksFine方法,函數的定義是內的明確專門的類定義,然後一切都很好。但對於問題的方法,當我定義明確專門的類定義之外的方法,我得到錯誤C2910
這是爲什麼?錯誤C2910表示問題是Test :: Problem()已經定義。但它不是定義在類內部......有沒有函數定義只是一個聲明。
根據您選擇放置函數定義的位置,可以做某件事情似乎很蹩腳,但我總是更傾向於風格/語法決策,而不是功能/語義決策。我錯過了什麼嗎?
謝謝,那確實有效! –