帮忙看看c++异常捕获try{}catch{}throw为什么throw不能返回一个具体对象

木兮₀₃₂₁ 木兮₀₃₂₁ 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
请诸位看一下为什么会这样!
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这两个对象了吗?
到底怎么回事?
求解
...全文

相关推荐

评论 6
收藏 已收藏
分享
返回顶部
木兮₀₃₂₁
C++ 语言 65,212 成员 250,513 内容