//a.cpp
class A{
public:
int fun(int x){
return (x*x+);
}
};
void tt()
{ } //b.cpp
class A{
public:
int fun(int x);
};
void tt();
int yy()
{
tt();
A a;
return a.fun();
}
将它们分别编译后再链接:
显示链接错误,因为b.cpp(b.o)中找不到A::fun(int)的引用。
将以上的a.cpp改为如下所示:
class A{
public:
int fun(int x);
};
int A::fun(int x){
return (x*x+);
}
void tt()
{ }
再次编译a.cpp,不用再次编译b.cpp,a.o和b.o后链接,显示链接成功。
这样,第一次链接错误的原因就很明显了。
结论: 在类定义中的类成员函数实现有文件内部作用域,而在类定义外部的类实现有的是全局作用域。