我正在为模板类中有一个模板成员函数的语法而苦恼:
可能是重复问题:
如何在类定义之外定义模板类中的模板函数?
template <typename T> class Foo
{
void Bar(const T * t);
template <typename T2> void Bar(const T2 * t);
};
template <typename T> void Foo<T>::Bar(const T * t)
{
// ... no problem ...
}
template <typename T> void Foo<T>::Bar<typename T2>(const T2 * t)
{
// ... this is where I'm tearing my hair out ...
}
第一个成员函数很好,但是处理模板类基本类型以外的类型的模板成员函数是我遇到问题的地方。对于上述情况,我收到以下错误信息:
template_problem.cpp:12: error: parse error in template argument list
template_problem.cpp:12: error: expected ‘,’ or ‘...’ before ‘*’ token
template_problem.cpp:12: error: ISO C++ forbids declaration of ‘T2’ with no type
template_problem.cpp:12: error: template-id ‘Bar<<expression error> >’ in declaration of primary template
template_problem.cpp:12: error: prototype for ‘void Foo<T>::Bar(int)’ does not match any in class ‘Foo<T>’
template_problem.cpp:4: error: candidates are: template<class T> template<class T2> void Foo::Bar(const T2*)
template_problem.cpp:7: error: void Foo<T>::Bar(const T*)
template_problem.cpp:12: error: template definition of non-template ‘void Foo<T>::Bar(int)’
我还尝试了我能想到的Bar
模板版本的所有语法变化。
template<typename T>
和template<typename T2>
这两行可以交换位置吗? - hkBattousaiFoo<T>
改成Foo<T2>
,那么顺序就很重要。 - jrok