下面的代碼編譯正確。模板專業化:非內聯函數的定義問題
#include <string>
template <typename T, typename U>
class Container
{
private:
T value1;
U value2;
public:
Container(){}
void doSomething(T val1, U val2);
};
template<typename T, typename U>
void Container<typename T, typename U>::doSomething(T val1, U val2)
{
; // Some implementation
}
template <>
class Container<char, std::string>
{
private:
char value1;
std::string value2;
public:
Container(){}
void doSomething(char val1, std::string val2)
{
; // Some other implementation
}
};
但是,如果我嘗試之外定義void doSomething(char val1, std::string val2)
,我碰到下面的錯誤。
#include <string>
template <typename T, typename U>
class Container
{
private:
T value1;
U value2;
public:
Container(){}
void doSomething(T val1, U val2);
};
template<typename T, typename U>
void Container<typename T, typename U>::doSomething(T val1, U val2)
{
; // Some implementation
}
template <>
class Container<char, std::string>
{
private:
char value1;
std::string value2;
public:
Container(){}
void doSomething(char val1, std::string val2);
};
template<>
void Container<char,std::string>::doSomething(char val1, std::string val2)
{
; // Some other implementation
}
錯誤:
Error 1 error C2910: 'Container::doSomething' : cannot be explicitly specialized c:\users\bharani\documents\visual studio 2005\projects\templates\template specialization\templatespecializationtest.cpp 35
我犯了什麼錯誤?
謝謝。
非常感謝!它幫助! – bdhar 2010-08-27 17:14:25