请诸位看一下为什么会这样!
exc_mean.hpp
exc_mean.hpp
#include<iostream> using namespace std; class bad_hmean { private: double v1; double v2; public: bad_hmean(int a=0,int b=0):v1(a),v2(b){} void mesg(); }; inline void bad_hmean::mesg() { cout<<"heman("<< v1 <<" , "<<v2<<"): " <<"invalid arguments a=-b \n"; } class bad_gmean { public: double v1; double v2; bad_gmean(double a=0,double b=0):v1(a),v2(b){} const char* mesg(); }; inline const char* bad_gmean::mesg() { return "gmean () srguments should be >0 \n"; }
error4.cpp
#include<iostream> #include<cmath> #include"exc_mean.hpp" using namespace std; double hmean(double a,double b); double gmean(double a,double b); int main() { double x,y,z; cout<<"Enter two numbers: "; while(cin>>x>>y) { try { z=hmean(x,y); cout<<"Harmonic mean of "<<x<<" and "<<y<<" is "<<z<<endl; cout<<"Geometric mean of "<<x<<" and "<<y<<" is "<<gmean(x,y)<<endl; cout<<"enter next set of numbers <q to quit> : "; } catch(bad_hmean &bh) { bh.mesg(); cout<<"Try again! \n"; continue; } catch(bad_gmean &bg) { cout<<bg.mesg(); cout<<"Values uesd "<<bg.v1<<" , "<<bg.v2<<endl; cout<<"Sorry ,you don"t play again. \n"; break; } } cout<<"Bye! \n"; return 0; } double hmean(double a,double b) { if(a==-b) //原句如下,编译运行没问题 //将此句替换成右边的两句,编译错误 // bad_hmean A=bad_hmean(a,b); throw bad_hmean(a,b); //throw A; return 2.0*a*b/(a+b); } double gmean(double a, double b) { if(a<0||b<0) throw bad_gmean(a,b); /* bad_gmean B=bad_gmean(a,b);// throw B;*/ return sqrt(a*b); }
报错如下:
error4.cpp:50:11: error: use of undeclared identifier “A”
throw A;
^
error4.cpp:57:11: error: use of undeclared identifier “B”
throw B;
^
2 errors generated.
怎么还说使用未声明的标示符 A和B
本人不是已经创建了A B这两个对象了吗?
到底怎么回事?
求指导
解决方案
2
哪个编译器这么蛋疼?
2
不是吧,什么版本?本人试了gcc没有这问题啊.规范定义的应该是throw expression;没有问题
14
if(a==-b) bad_hmean A=bad_hmean(a,b); throw A;
这种代码不报错才是怪事
起码加上大括号吧
if(a==-b){ bad_hmean A=bad_hmean(a,b); throw A; }
4
if 后假如不加括号,只对后一条语句有效,这样你的定义是在if里,但throw不是在if里,故找不到,加上大括号就好了