帮忙看看c++异常捕获try{}catch{}throw为什么throw不能返回一个具体对象
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这两个对象了吗?
到底怎么回事?
求解
相关推荐
青x科技大学软件专业面向对象课程(C++)复习材料
花费了好久整理的面向对象课程复习材料,保证考试碰到原题!题目都是老师划重点画出来的,考试就考这些题目!
JAVA中的关键字和保留字
abstract - 1 - boolean - 2 - break - 2 - byte - 2 - case - 3 - catch - 3 - char - 4 - class - 4 - continue - 5 - default - 5 - do - 6 - double - 6 - else - 6 - extends - 6 - false - 7 - final - 7 - finally - 7 - float - 8 - for - 8 - if - 8 - implements - 9 - import - 9 - instanceof - 9 - int - 9 - interface - 10 - long - 10 - native - 10 - new - 11 - null - 11 - package - 11 - private - 11 - protected - 12 - public - 12 - return - 13 - short - 13 - static - 13 - super - 14 - switch - 14 - synchronized - 15 - this - 16 - throw - 16 - throws - 16 - transient - 17 - try - 17 - true - 18 - void - 18 - volatile - 18 - while - 18 -
北语18春《c++》.docx
。
C++上机全真模拟原文件-含答案
C++上机全真模拟原文件-含答案
数据结构与算法C++.
数据结构与算法的C++版,电子档pdf格式,很全面的资料啊。


