读书人

C++错误处理有关问题

发布时间: 2012-03-26 15:46:55 作者: rapoo

C++异常处理问题
如下程序是未了看看异常情况.
要是底数为负,指数为小数时会抛出异常.....

1.
//==============================================================
// power.h -- Declarations for power() function and TPowerExcept class
//==============================================================
#ifndef __power_H
#define __power_H // Prevent multiple #includes
#include <exception> // Need exception class
#include <string> // Need string class
//好像对继承exception不认???
class TPowExcept:public exception {
double b; // Base value
double e; // Exponent value
string what_str; // Error description
public:
TPowExcept(double b_arg, double e_arg, const string &what_arg):
b(b_arg), e(e_arg), what_str(what_arg) { }
virtual const char *what() const {
return what_str.c_str();
}
void Report();
};

double power(double b, double e) throw(TPowExcept);

#endif // __power_H

2.
//==============================================================
// power.cpp -- Implements the power() function and TPowerExcept class
// g++ -c power.cpp
//==============================================================

#include <iostream.h>
#include <math.h> // Need modf(), fmod()
#include "power.h " // Need TPowExcept, power()
// Display error message in exception object
void TPowExcept::Report()
{
cout < < "Domain error: base == " < < b
< < ", exponent == " < < e < < endl;
cout < < what() < < endl;
}

// Subfunction called by power()
double fpower(double b, double e)
{
return exp(e * log(b));
}

// Returns b raised to the e power
// Throws TPowExcept exception for illegal input values
double power(double b, double e) throw(TPowExcept)
{
if (b > 0.0) return fpower(b, e);
if (b < 0.0) {
double ipart;
double fpart = modf(e, &ipart);
if (fpart == 0) {


if (fmod(ipart, 2) != 0) // i.e. ipart is odd
return -fpower(-b, e);
else
return fpower(-b, e);
} else
throw TPowExcept(b, e, "Result is a complex number ");
} else {
if (e == 0.0) return 1.0;
if (e < 1.0)
throw TPowExcept(b, e, "Exponent must be zero or > = 1.0 ");
return 0.0;
}
throw TPowExcept(0, 0, "Error in power() function ");
}
3.
//==============================================================
// tpower.cpp -- Tests the power() function and TPowExcept class
//==============================================================

#include <iostream.h>
#include "power.h " // Need TPowExcept, power()
int main()
{
double base, exponent, result; // Input and result variables
try {
cout < < "base? ";
cin > > base;
cout < < "exponent? ";
cin > > exponent;
result = power(base, exponent); // Exception possible here
cout < < "result == " < < result < < endl;
}
catch (TPowExcept &except) {
except.Report(); // Display error message
return 1; // Exit with error
}
return 0; // Exit with no error
}
我用g++ -c power.cpp
编译时出现如下错误:


In file included from power.cpp:12:
power.h:13: error: parse error before `{ ' token
power.h:16: error: 'string ' is used as a type, but is not defined as a type.
power.h:17: error: parse error before `public '
power.h:20: error: virtual outside class declaration
power.h:20: error: non-member function `const char* what() ' cannot have `const '
method qualifier
power.h: In function `const char* what() ':


power.h:21: error: `what_str ' undeclared (first use this function)
power.h:21: error: (Each undeclared identifier is reported only once for each
function it appears in.)
power.h: At global scope:
power.h:24: error: parse error before `} ' token
power.h:26: error: invalid use of undefined type `class TPowExcept '
power.h:13: error: forward declaration of `class TPowExcept '
power.cpp:15: error: invalid use of undefined type `class TPowExcept '
power.h:13: error: forward declaration of `class TPowExcept '
power.cpp: In member function `void TPowExcept::Report() ':
power.cpp:17: error: `b ' undeclared (first use this function)
power.cpp: At global scope:
power.cpp:29: error: invalid use of undefined type `class TPowExcept '
power.h:13: error: forward declaration of `class TPowExcept '
power.cpp: In function `double power(double, double) ':
power.cpp:41: error: invalid use of undefined type `class TPowExcept '
power.h:13: error: forward declaration of `class TPowExcept '
power.cpp:45: error: invalid use of undefined type `class TPowExcept '
power.h:13: error: forward declaration of `class TPowExcept '
power.cpp:48: error: invalid use of undefined type `class TPowExcept '
power.h:13: error: forward declaration of `class TPowExcept '
power.cpp:49: error: invalid use of undefined type `class TPowExcept '
power.h:13: error: forward declaration of `class TPowExcept '


[解决办法]
在#include <string> 后面加using namespace std;
[解决办法]
提示不是很清楚嘛,你缺了后面的throw (),这个也是函数签名的一部分。
[解决办法]
/usr/include/c++/3.3/exception 文件中有..
virtual ~exception() throw();

所以你需要定义析构函数.....
加上~TPowExcept() throw() { }

再把virtual const char *what() const
const 去掉.

读书人网 >C++

热点推荐