任何人都可以告诉这段代码有什么问题吗?
template<class X>
class C {
public:
template<class Y> void f(Y); // line 4
};
template<class X, class Y>
void C<X>::f(Y y) { // line 8
// Something.
}
int main() {
C<int> c;
char a = 'a';
c.f(a);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
汇编:
$ g++ source.cpp
source.cpp:8: error: prototype for ‘void C<X>::f(Y)’ does not match any in class ‘C<X>’
source.cpp:4: error: candidate is: template<class X> template<class Y> void C::f(Y)
Run Code Online (Sandbox Code Playgroud)
我现在可以通过在第4行声明和定义函数来完成任务,但与单独执行相比,同时声明和定义函数的后果是什么?(这不是关于在头文件和源文件中声明函数的讨论)
注意:我已经看到了这个问题,但似乎我感兴趣的唯一部分是分开=(
Ker*_* SB 15
编译器已经告诉你答案了.该类C是一个带有一个参数的模板,成员函数f是模板成员函数,您必须以相同的方式定义它:
template <class X>
template <class Y>
void C<X>::f(Y y)
{
// Something.
}
Run Code Online (Sandbox Code Playgroud)
如果在声明站点定义函数,则会隐式声明它inline; 这实际上是唯一的区别.当然,可能存在风格因素,例如,永远不要将函数定义放在类定义中.
正如您正确观察到的那样,您仍然需要在头文件中提供定义,因为您需要在使用它时实例化模板,因此您需要访问所有定义.