【发布时间】:2011-10-24 07:39:34
【问题描述】:
谁能说出这段代码有什么问题?
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,但似乎唯一让我感兴趣的部分被遗漏了=(
【问题讨论】: