Exceptions Classes for Zero and Negitive

Receiving errors and I am not sure why. I am attempting to add exception files for zero and negative. My program was working until I attempted to add these files. The error: expected class-name before '{' token|, has me really scratching my head.

||=== Build: Debug in Final Area (compiler: GNU GCC Compiler) ===|
NegativeException.h|8|error: expected class-name before '{' token|
NegativeException.h|10|error: 'string' does not name a type|
NegativeException.h|12|error: expected ')' before '_msg'|
NegativeException.h||In member function 'virtual const char* NegativeException::what() const':|
NegativeException.h|19|error: 'msg' was not declared in this scope|
ZeroException.h|6|error: expected class-name before '{' token|
ZeroException.h|8|error: 'string' does not name a type|
ZeroException.h|10|error: expected ')' before '_msg'|
ZeroException.h||In member function 'virtual const char* ZeroException::what() const':|
ZeroException.h|17|error: 'msg' was not declared in this scope|
Circle.cpp|18|error: expected unqualified-id before '{' token|
||=== Build failed: 9 error(s), 0 warning(s) (0 minute(s), 0 second(s)) ===|


Last edited on
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
#include <iostream>
#include <string>
#include "Circle.h"  //Defines Circle
#include "Square.h"  //Defines Square
#include "Rectangle.h" //Defines Rectangle



int main()
{   // Start main function
using namespace std;

 areaChoice = 0;
 int menuOption;



		if(areaChoice > 3)
            {
			cout<<"You did not enter a valid option"<<endl;
			cout<<"Please re-enter a valid option, 1, 2 or 3: ";
			cin>>areaChoice;
			cout<<endl;
            }

	  else if (areaChoice <=3)
            {
			cout<<"Thank you for your entry."<<endl;
            }


    //Array requires user to select 1 for Circle or 2 for Square 3 for Rectangle
    do
    { // Starting of while loop
       cout << endl << "_______________Calculate Area______________" << endl;
       cout << "Select an Object" << endl;
       cout << " 1: Circle" << endl;
       cout << " 2: Square" << endl;
       cout << " 3: Rectangle" <<endl;
       cout << " 0: Exit" << endl;
       cout << " Enter Your Choice: ";
       cin  >> menuOption;


      switch(menuOption)

{

case 1:
{
    double radius;
    cout << "Enter the radius in inches. ";
    cin >> radius;
    string name = "myCircle";
    string unit = "in";
    Circle C(name, radius, unit);
    cout << "The area of the circle is " << C.getArea() << " inches." << endl;
    break;
}

case 2:
{
    double length;
    cout << "Enter the length of one side in inches. ";
    cin >> length;
    string name = "mySquare";
    string unit = "in";
    Square S(name,length,unit);
    cout << "The area of the square is " << S.getArea() << " inches." << endl;
    break;
}

case 3:

    {
    double length;
    double width;

        cout << "Enter the length of one side in inches. ";
        cin >> length;
        cout << "Enter the width of one side in inches. ";
        cin >> width;
        string name = "myRectangle";
        string unit = "in";
        Rectangle R(name,length, width, unit);
        cout << "The area of the rectangle is " << R.getArea() << " inches." << endl;
    }
}


}
   while(menuOption!=0);

   cout<<" ________________ THANK YOU _______________" <<endl;

    return 0;
}   // This ends the main function
are you sure you have not missed any brackets?
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
#ifndef CIRCLE_H_INCLUDED
#define CIRCLE_H_INCLUDED
#include "Shape.h"

class Circle : public Shape      //Class Circle inherits from Base Class Shape
{
//Member declarations
    private:

        double radius;

// Constructors
    public:

        Circle(std::string &, double, std::string &);

// Parent Virtual function

        double getArea() const;

//Function prototypes

        double getRadius();     //Get the Radius (getter)
        void setRadius(double);   //Sets Radius (setter)
};



#endif // CIRCLE_H_INCLUDED


#include "Circle.h"
#include "NegativeException.h"
#include "ZeroException.h"

using namespace std;

const double PI = 3.14159;

//Set radius
 Circle::Circle (string &name, double r, string &unit) :
    Shape(name, "Circle", unit)
    {
        radius = r;
    }



{
	try
	{
		cout<<"Enter the circle's radius: ";
		cin>>radius;
		if (radius < 0)
		{
			throw NegativeException("ERROR: Negative number entered.");
		}
		else if (radius == 0)
			throw ZeroException("ERROR: Zero entered");

	}
	catch (ZeroException ze)
	{
		radius = 0;
		cout << ze.what();
	}
	catch(NegativeException ne)
	{
		radius = 0;
		cout << ne.what();
	}

}

//calculate area

    double Circle::getRadius() { return radius; }

    double Circle::getArea() const
    {
        return (PI * (radius * radius));
    }

    void Circle::setRadius(double r) { radius = r; }


#include "Square.h"
#include "NegativeException.h"
#include "ZeroException.h"

using namespace std;

//Set length
Square::Square(string &name, double len, string &unit) :
    Shape(name, "Square", unit)
    {
        length = len;
    }

//Calculate Area
    double Square::getArea() const
    {
        return length * length;
    }

{
	try
	{
		cout<<"Enter Square length: ";
		cin>>length;
		if (length < 0 )
			throw NegativeException("ERROR: Negative number entered.");
		else if (length == 0)
			throw ZeroException("ERROR: Zero entered");

	}
	catch (ZeroException ze)
	{
		length = 0;
		cout << ze.what();
	}
	catch(NegativeException ne)
	{
		length = 0;
		cout << ne.what();
	}

}

    double Square::getLength() const { return length; }

    void Square::setLength(double len){ length = len; }

#ifndef SQUARE_H_INCLUDED
#define SQUARE_H_INCLUDED
#include "Shape.h"

class Square : public Shape   //Class Square inherits from Base Class Shape
{

//Member declarations
    private:

        double length;

//Constructors

    public:

        Square(std::string &, double, std::string &);

//Parent Virtual function

        double getArea() const;

//Function prototypes

        double getLength() const;     //Get length (getter)
        void setLength(double);      //Set length (setter)
};

#endif // SQUARE_H_INCLUDED


#include "Rectangle.h"
#include "NegativeException.h"
#include "ZeroException.h"

using namespace std;

//Set length, width
Rectangle::Rectangle(string &name, double len, double wid, string &unit) :
    Shape(name, "Rectangle", unit)
    {
        length = len;
        width = wid;
    }

try
	{
		cout<<"Enter Rectangle length: ";
		cin>>length;
		cout<<"Enter Rectangle width: ";
		cin>>width;
		if (length < 0 || width < 0)
			throw NegativeException("ERROR: Negative number entered.");
		else if (length == 0 || width == 0)
			throw ZeroException("ERROR: Zero entered");

	}
	catch (ZeroException ze)
	{
		length = 0;
		width = 0;
		cout << ze.what();
	}
	catch(NegativeException ne)
	{
		length = 0;
		width = 0;
		cout << ne.what();
	}

}
//Calculate Area
    double Rectangle::getArea() const
    {
        return length * width;
    }

    double Rectangle::getLength() const { return length;}
    double Rectangle::getWidth() const {return width;}

    void Rectangle::setLength(double len) { length = len;}
    void Rectangle::setWidth(double wid) {width = wid;}
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
#ifndef RECTANGLE_H_INCLUDED
#define RECTANGLE_H_INCLUDED
#include "Shape.h"

class Rectangle : public Shape      //Class Rectangle inherits from Base Class Shape
{
//Member declarations
    private:

        double length;
        double width;

//Constructors

    public:

        Rectangle(std::string &, double,double, std::string &);

// Parent Virtual function

    double getArea() const;

//Function prototypes

    double getLength() const; //Get length
    double getWidth() const; //Get width

    void setLength(double); //Set length
    void setWidth(double); //Set width
};




#endif // RECTANGLE_H_INCLUDED


#include "Shape.h"
#include <string>

Shape::Shape() {}

Shape::Shape(std::string &name, const std::string &shape, std::string &unit)
{
    shapeID = name;
    shapeName = shape;
    unitOfMeasure = unit;
}
    std::string Shape::getShapeID() const
    { return shapeID; }

    std::string Shape::getShapeName() const
    { return shapeName; }

    std::string Shape::getUnitOfMeasure() const
    { return unitOfMeasure; }

    void Shape::setShapeID(std::string &name)
    { shapeID = name; }

    void Shape::setShapeName(std::string &shape)
    { shapeName = shape; }

    void Shape::setUnitOfMeasure(std::string &unit)
    { unitOfMeasure = unit; }


#ifndef SHAPE_H_INCLUDED
#define SHAPE_H_INCLUDED
#include <string>

class Shape        //Class Name
{

//Member declarations
    private:

        std::string shapeID;        //name of instance
        std::string shapeName;      //Circle, Square, Rectangle
        std::string unitOfMeasure;  //Centimeters or Inches

//Member prototypes
    public:

        Shape();

// derived class constructor will pass a constant string to the base class' constructor.

        Shape(std::string &, const std::string &, std::string &);

//The "get" functions will not allowed to modify any variables in the class.

        std::string getShapeID() const;
        std::string getShapeName() const;
        std::string getUnitOfMeasure() const;

//Each derived class must provide its own getArea().

        virtual double getArea() const = 0;

        void setShapeID(std::string &);
        void setShapeName(std::string &);
        void setUnitOfMeasure(std::string&);
        void print();           //Prints the information
};

#endif // SHAPE_H_INCLUDED


#include <exception>
#include <string>
#ifndef ZeroException_H
#define ZeroException_H

class ZeroException: public exception
{
	private:
		string msg;
public:
		ZeroException(string _msg)
	{
		msg=_msg;
	}

	  virtual const char* what() const throw()
	  {
		return msg.c_str();
	  }
};


#endif


#include <exception>
#include <string>
#ifndef NEGATIVEEXCEPTION_H_INCLUDED
#define NEGATIVEEXCEPTION_H_INCLUDED

class NegativeException: public exception

{
	private:
	string msg;
public:
	NegativeException(string _msg)
	{
		msg=_msg;
	}

	  virtual const char* what() const throw()
	  {
		return msg.c_str();
	  }
} ;


#endif // NEGITIVEEXCEPTION_H_INCLUDED 
on your last message:
possible
line 49 open '{' ;
line 73 close '}' ;

inside an exception it has been defined "try - catch" .. but what member function ?

the same on line 106 and 128 and the same on line 179 and 204
Last edited on
Member function... hmm I tried void Square:: getArea(). Not working
I am still having issues with adding the exception classes to my program. Not sure why all these errors are coming up. I don't know where to look.

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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196

#include <iostream>
#include <string>
#include "Circle.h"  //Defines Circle
#include "Square.h"  //Defines Square
#include "Rectangle.h" //Defines Rectangle
#include "Shape.h"
#include "NegativeException.h"
#include "ZeroException.h"


int main()
{   // Start main function
using namespace std;

 int areaChoice;
 int menuOption;

    while(areaChoice >3)
    {
    cout<<"You didn't enter a valid option"<<endl;
    cout<<"Please re-enter a valid option, 1, 2 or 3: ";
    cin>>areaChoice;
    cout<<endl;
    }



        //Array requires user to select 1 for Circle or 2 for Square 3 for Rectangle
        do
        { // Starting of while loop
           cout << endl << "_______________Calculate Area______________" << endl;
           cout << "Select an Object" << endl;
           cout << " 1: Circle" << endl;
           cout << " 2: Square" << endl;
           cout << " 3: Rectangle" <<endl;
           cout << " 0: Exit" << endl;
           cout << " Enter Your Choice: ";
           cin  >> menuOption;
        }

        while(areaChoice >3);

      Shape *shape;    //Expression: Shape base class
      switch(menuOption)
        {

        case 1:
        {
            double radius;
            shape->getInput();   //Get user input
            cout << "Enter the radius in inches. ";
            cin >> radius;
            string name = "myCircle";
            string unit = "in";
            Circle C(name, radius, unit);
            cout << "The area of the circle is " << C.getArea() << " inches." << endl;
            break;
        }

        case 2:
        {
            double length;
            shape->getInput();   //Get user input
            cout << "Enter the length of one side in inches. ";
            cin >> length;
            string name = "mySquare";
            string unit = "in";
            Square S(name,length,unit);
            cout << "The area of the square is " << S.getArea() << " inches." << endl;
            break;
        }

        case 3:

            {
            double length;
            double width;
            shape->getInput();   //Get user input
            cout << "Enter the length of one side in inches. ";
            cin >> length;
            cout << "Enter the width of one side in inches. ";
            cin >> width;
            string name = "myRectangle";
            string unit = "in";
            Rectangle R(name,length, width, unit);
            cout << "The area of the rectangle is " << R.getArea() << " inches." << endl;
            }
}//switch



   while(menuOption!=0);

   cout<<" ________________ THANK YOU _______________" <<endl;

    return 0;
}   // This ends the main function


//Rectangle.cpp

#include <iostream>
#include <string>
#include "Circle.h"  //Defines Circle
#include "Square.h"  //Defines Square
#include "Rectangle.h" //Defines Rectangle
#include "Shape.h"
#include "NegativeException.h"
#include "ZeroException.h"


//Main Rectangle Function Call
Rectangle::Rectangle()
{
	width = 0;
	height = 0;
	setShapeType("Rectangle");
}

//Obtain User Input
void Rectangle::getInput()
{


	try
	{
		cout<<"Enter Rectangle height: ";
		cin>>height;
		cout<<"Enter Rectangle width: ";
		cin>>width;
		if (height < 0 || width < 0)
			throw NegativeException("ERROR: Negative number entered.");
		else if (height == 0 || width == 0)
			throw ZeroException("ERROR: Zero entered");

	}
	catch (ZeroException ze)
	{
		height = 0;
		width = 0;
		cout << ze.what();
	}
	catch(NegativeException ne)
	{
		height = 0;
		width = 0;
		cout << ne.what();
	}

}

//Area calculated for Rectangle based on User Input
double Rectangle::area()
{
	return width * height;
}


//Rectangle.h

#ifndef RECTANGLE_H_INCLUDED
#define RECTANGLE_H_INCLUDED
#include "Shape.h"

class Rectangle : public Shape      //Class Rectangle inherits from Base Class Shape
{
//Member declarations
    private:

        double length;
        double width;

//Constructors

    public:

        Rectangle(std::string &, double,double, std::string &);

// Parent Virtual function

    double getArea() const;
    void getInput();
//Function prototypes

    double getLength() const; //Get length
    double getWidth() const; //Get width

    void setLength(double); //Set length
    void setWidth(double); //Set width
};




#endif // RECTANGLE_H_INCLUDED 

Errors

||=== Build: Debug in Final Area (compiler: GNU GCC Compiler) ===|
main.cpp||In function 'int main()':|
main.cpp|50|warning: 'shape' may be used uninitialized in this function [-Wmaybe-uninitialized]|
Rectangle.cpp|12|error: prototype for 'Rectangle::Rectangle()' does not match any in class 'Rectangle'|
Rectangle.h|5|error: candidates are: Rectangle::Rectangle(Rectangle&&)|
Rectangle.h|5|error: Rectangle::Rectangle(const Rectangle&)|
Rectangle.h|17|error: Rectangle::Rectangle(std::string&, double, double, std::string&)|
Rectangle.cpp||In member function 'virtual void Rectangle::getInput()':|
Rectangle.cpp|26|error: 'cout' was not declared in this scope|
Rectangle.cpp|26|note: suggested alternative:|
C:\Program Files (x86)\CodeBlocks\MinGW\lib\gcc\mingw32\4.9.2\include\c++\iostream|61|note: 'std::cout'|
Rectangle.cpp|27|error: 'cin' was not declared in this scope|
Rectangle.cpp|27|note: suggested alternative:|
C:\Program Files (x86)\CodeBlocks\MinGW\lib\gcc\mingw32\4.9.2\include\c++\iostream|60|note: 'std::cin'|
Rectangle.cpp|27|error: 'height' was not declared in this scope|
Rectangle.cpp|38|error: 'height' was not declared in this scope|
Rectangle.cpp|40|error: 'cout' was not declared in this scope|
Rectangle.cpp|40|note: suggested alternative:|
C:\Program Files (x86)\CodeBlocks\MinGW\lib\gcc\mingw32\4.9.2\include\c++\iostream|61|note: 'std::cout'|
Rectangle.cpp|44|error: 'height' was not declared in this scope|
Rectangle.cpp|46|error: 'cout' was not declared in this scope|
Rectangle.cpp|46|note: suggested alternative:|
C:\Program Files (x86)\CodeBlocks\MinGW\lib\gcc\mingw32\4.9.2\include\c++\iostream|61|note: 'std::cout'|
Rectangle.cpp|52|error: no 'double Rectangle::area()' member function declared in class 'Rectangle'|
||=== Build failed: 12 error(s), 1 warning(s) (0 minute(s), 1 second(s)) ===|
1-insert: Rectangle(); on Rectangle.h
2-insert: using namespace std; on Rectangle.cpp
3-insert: data member private 'heigth' on class Rectangle , on Rectangle.h
4-insert: member function area on class Rectangle , in Rectangle.h
Topic archived. No new replies allowed.