我要實現模板專業化,在實現專用模板類的構造函數時編譯器會產生一些錯誤。以下是我的代碼:模板專業化實現
#include <iostream>
using namespace std;
// class template
template <typename T>
class mycontainer
{
T element;
public:
mycontainer (T arg);
T increase() {return ++element;}
};
// class template specialization
template <>
class mycontainer <void> {
int element;
public:
mycontainer (int arg);
char uppercase()
{
return element;
}
};
template<typename T> mycontainer<T>::mycontainer(T arg){
cout << "hello T" << endl;
}
template<typename T> mycontainer<void>::mycontainer(int arg){
cout << "hello Empty" << endl;
}
int main() {
mycontainer<int> myint (7);
mycontainer<void> myvoid (6);
cout << myint.increase() << endl;
return 0;
}
的代碼生成這些錯誤:
test.cpp:31:22: error: prototype for ‘mycontainer<void>::mycontainer(int)’ does not match any in class ‘mycontainer<void>’
test.cpp:16:26: error: candidates are: mycontainer<void>::mycontainer(const mycontainer<void>&)
test.cpp:19:5: error: mycontainer<void>::mycontainer(int)
就如何解決這些錯誤的任何線索?