template <class T>
class Test
{
public:
template<class T>
void f(); //If i define function here itself, error is not reported.
};
template <class T>
void Test<T>::f()
{
} //Error here.
int main()
{
Test<float> ob;
ob.f<int>();
}
它产生以下错误。
error C2244: 'Test<T>::f' : unable to match function definition to an existing declaration
定义'void Test::f(void)'
现有声明 'void Test::f(void)'
错误说声明和定义具有相同的原型(prototype)但不匹配。 为什么这是一个错误?以及如何解决?
如果我在类中定义函数,它不会报告错误。 但我想在类外定义。
最佳答案
改变为
template <class T>
class Test
{
public:
template<class U>
void f();
};
template <class T>
template<class U>
void Test<T>::f()
{
}
关于c++ - 使用模板时的编译错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4061741/