Try-Catch(bad_alloc) [i cant find my fault]

i have an exercise in my Uni and i am a little bit confused about the try-catch exception...

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
class Pilot                      //Class Pilot
{
public:

struct ExperAircraft{
char	xaAircraftType[20];      //AircraftType of the pilot's experience 
double	xaFlightTime;            //FlightTime of each AircraftType 
		     };
Pilot()                          
{
try              {piExper=new ExperAircraft[piIncr];}
catch (bad_alloc){throw PilotXptn("NoMemory",PilotXptn::NoMemory);}
}
private:

unsigned int	piIdNum;         //Pilot Id
char		piSurname[20];   //PilotSurname
char		piFirstName[16]; //PilotFirstname
ExperAircraft*	piExper;         //Dynamic array of Pilot's Experience
unsigned int	piNoOfExper;     //How many values has the piExper array
enum		{piIncr=8};      //When piExper needs more memory asks for 8 !
};

class PilotXptn
{
public:

enum 	{NoMemory};
PilotXptn(std::string FN,int eC=0,int eV=0)
{FunctionName=FN; errorCode=eC; errorValue=eV;}

std::string 	getFunctionName() const {return FunctionName;}
int	        getErrorCode()    const {return errorCode;}
int	        getErrorValue()	  const {return errorValue;}

private:

std::string	FunctionName;
int	        errorCode;
int		errorValue;

};


Well i need a class named : Pilot .and at the Constructor i must use the PilotXptn class to control the exceptions( NoMemory when piExper asks for)

My Problem is that i think i did the 2 classes correct but the compiler disagree .giving me the following messages:
1)Type Name expected line 12
2)Catch Statement missing ) line 12
3)Compound statement missing } line 13

PS:i am using the Borland C++ compiler.
std::bad_alloc

std::bad_alloc didnt fixed it

:/ any other idea?
Did you include the appropriate header file for std::bad_alloc?
Include iostream
iostream might solve the problem but that is not the right header. it is either exception or stdexcept, I can't remember which.
my includes are :

#include <iostream.h>
#include <fstream.h>
#include <string.h>
#include <new.h>

still didnt fix ! :(
closed account (z05DSL3A)
Probably a silly question but have you declared PilotXptn prior to using it in Pilot?

Edit: Forward declaration will not cut it because of PilotXptn::NoMemory, try:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
class PilotXptn
{
public:

    enum 	{NoMemory};
    PilotXptn(std::string FN,int eC=0,int eV=0)
    {
        FunctionName=FN; 
        errorCode=eC; 
        errorValue=eV;
    }

    std::string     getFunctionName() const {return FunctionName;}
    int	            getErrorCode()    const {return errorCode;}
    int	            getErrorValue()	  const {return errorValue;}

private:

    std::string	FunctionName;
    int	        errorCode;
    int         errorValue;

};

class Pilot                      //Class Pilot
{
public:

    struct ExperAircraft
    {
        char	xaAircraftType[20];      //AircraftType of the pilot's experience 
        double	xaFlightTime;            //FlightTime of each AircraftType 
    };

    Pilot()                          
    {
        try              
        {
            piExper=new ExperAircraft[piIncr];
        }
        catch (std::bad_alloc)
        {
            throw PilotXptn("NoMemory",PilotXptn::NoMemory);
        }
    }
private:

    unsigned int	piIdNum;         //Pilot Id
    char		piSurname[20];   //PilotSurname
    char		piFirstName[16]; //PilotFirstname
    ExperAircraft*	piExper;         //Dynamic array of Pilot's Experience
    unsigned int	piNoOfExper;     //How many values has the piExper array
    enum		{piIncr=8};      //When piExper needs more memory asks for 8 !
};
Last edited on
its only new and not new.h
@Grey Wolf :
This is not the problem coz i think when we r writing functions classes etc the ProgramCounter jumps to the exact memory where each function,class starts ! so we need to keep the priority only inside the functions ( Pilot , PilotXptn ,main() ) .i hope u understand me what i want to say ( my english sux a bit hehe ).although i tried it...same happens !
@writetonsharma:
i think the new file is a header file so *.h is not the problem in my compiler ... although i tried #include <new> and same happens !

i repeat the compiler's messages :
1)Type Name expected line 12
2)Catch Statement missing ) line 12
3)Compound statement missing } line 13

well any other idea will be welcome...
if i wont solve it in 2 days i ll visit my professor so i ll post the solution in my problem here ! :P



Please post your complete source file (unedited) and the complete compile errors (unedited).
Topic archived. No new replies allowed.