有人能告诉我这段代码有什么问题吗?
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;
}
编译:
$ 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)
我知道可以在第4行同时声明和定义函数来完成任务,但与分别进行声明和定义相比,同时进行声明和定义会有什么后果?(这不是关于在头文件与源文件中声明函数的讨论)
注意:我看到了this question,但似乎只有我感兴趣的部分被忽略了 =(
inline
部分(再次感谢=D)。我之前对此毫不知情。 - freitass