C++程序中出现 undeclared identifie怎么破?

#include <iostream>//函数功能:比较两个箱子B1与B2的大小using std::cout;using std::endl;
class CBox{public: CBox(double lv=1.0,double bv=1.0,double hv=1.0) { cout<<endl<<"Constructor called."; m_Length=lv; m_Width=bv; m_Heigth=hv; } double Volume()//箱子体积计算公式 { return m_Length*m_Width*m_Heigth;
} int Compare(CBox B1,CBox B2)//比较B1,B2的函数 { return B1.Volume()>B2.Volume(); }
private: double m_Length; double m_Width; double m_Heigth;
};
int main(){ CBox B1(2.2,1.1,0.5); CBox B2(8.0,5.0,1.0);
if(Compare(B1,B2)) cout<<endl <<"B1比B2大"; else cout<<endl <<"B1小于或等于B2";
cout<<endl; return 0;}
运行后出现这种结果。

C:\MSDev98\MyProjects\类的const对象\类的const对象.cpp(43) : error C2065: 'Compare' : undeclared identifier

自己太浅了,怎么分析都不知道哪里出问题了,我明明在类函数中定义了int Compare(CBox B1,CBox2)了呀,为什么会这样?

要声明类的对象然后调用。。

温馨提示:答案为网友推荐,仅供参考
第1个回答  2013-11-15
int Compare(CBox B1,CBox2)函数是类的成员函数 药用对象或者指针 引用来使用

CBox b;
b.Compare ();

楼主不如把 这个函数申明为这个类的友元 并且 用operator== 代替这个函数
第2个回答  2013-11-15
将Compare函数从类里面拿出来放到主函数前面即可。
int Compare(CBox B1,CBox B2)//比较B1,B2的函数
{
return B1.Volume()>B2.Volume();
}
int main()
{
CBox B1(2.2,1.1,0.5);
CBox B2(8.0,5.0,1.0);
if(Compare(B1,B2))
cout<<endl
<<"B1比B2大";
else
cout<<endl
<<"B1小于或等于B2";
cout<<endl;
return 0;
}
相似回答