Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Handling Exceptions

Status
Not open for further replies.

zulfi1234

Instructor
Jul 2, 2000
32
PK



I have created an Overdrawn class and an Account class. The Account class is generating an
exception of Overdrawn type when the amount to debit becomes greater than the current balance,
Can some body please look into my code & find out the error. The project has three files:
Account.h, Overdrawn.h & main.cpp

//main.cpp
#include "iostream.h"
#include "Account.h"
#include "iostream.h"
#include "Overdrawn.h"
void main ( ) {
Account acctobj (20.0f);
try {
acctobj.debit ( 100.0f);
}
catch (Overdrawn od){
cout <<&quot;OVERDRAWN&quot;;
};
}

//Account.h
#include &quot;Overdrawn.h&quot;
class Account {
float balance;
public:
Account (float initbalance ){
balance = initbalance;
}
float getBalance ( ) {
return balance;
}
void credit (float amount) {
balance +=amount;
}
void debit (float amount) {
if (balance - amount < 0)
throw Overdrawn();
else
balance -= amount;
}
};

//Overdrawn.h
class Overdrawn {
public:
Overdrawn ( ) {
cout <<&quot;Cant overdrawn&quot;;
}
};

I am getting the following error message:
d:\vc6_d\exceptions1\overdrawn.h(1) : error C2011: 'Overdrawn' : 'class' type redefinition

Zulfi
 
Dear Zulfi,

It sounds like you need to use some precompiler 'single inclusion' logic in your header files, i.e.:

//Overdrawn.h
#ifndef _OVERDRAWN_H
#define _OVERDRAWN_H

class Overdrawn {
public:
Overdrawn ( ) {
cout <<&quot;Cant overdrawn&quot;;
}
};

#endif

Hope this helps
-pete
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top