Issue with code

On my Circle.cpp and rectangle.cpp I am getting the that there is an error that says epected unqualified-id before '{' token before the try and both of the code. I am also having a separate issues with my ZeroException.h and negativeexception.h that says looser throw specifier for 'virtual ZeroException::~ZeroException. Also the error overriding 'virtual std::exception::~exception () throw()' these same error are also for the negativeexcption one as well. I have pasted all of my code below.
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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
  main.cpp

#include "main.h"
using namespace std;
int main(int argc, char**argv) {
    // Prints welcome message...

    double length = 0.0;
    //double slength = 0.0;
    //double swidth = 0.0;
    double width = 0.0;
    double radius = 0.0;
    string unitOfmeasure = "";

    for (int i = 0; i<3; i++)
    {
        try
        {

            unitOfmeasure = "";
            if(i == 0)
            {
                cout << "Please enter the unit of measurement for rectangle: "<< endl;
                cin >> unitOfmeasure;
                cout << "Please enter the width of the rectangle: "<< endl;
                cin >> width;
                cout << "Please enter the length of the rectangle"<< endl;
                cin >>  length;


                Shape * rect = new Rectangle(width,length, unitOfmeasure);
                    std::cout
                            <<"ID = "
                            << std::fixed << rect->getId()
                            <<", Type = "
                            << std::fixed << rect->getShapeType()
                            <<", Area  = "
                            << std::fixed << rect->getArea()
                            << std::fixed << rect->getunitOfmeasure()
                            <<std::endl;

            }
            else if(i == 2)
            {
                cout << "Please enter the unit of measurement for Square: "<< endl;
                cin >> unitOfmeasure;

                cout << "Please enter the length of the Square: "<< endl;
                cin >> length;
                //cout << "Please enter the width of the Square"<< endl;
                //cin >>  swidth;



                Shape * squ = new Rectangle(length, length, unitOfmeasure);
                std::cout
                        <<"ID = "
                        << std::fixed << squ->getId()
                        <<", Type = "
                        << std::fixed << squ->getShapeType()
                        <<", Area  = "
                        << std::fixed << squ->getArea()
                        << std::fixed << squ->getunitOfmeasure()
                        <<std::endl;

            }
            else
            {
                cout << "Please enter the unit of measurement for circle: "<< endl;
                cin >> unitOfmeasure;
                cout << "Please enter the radius of the circle: "<< endl;
                cin >> radius;

                Shape * cir=new Circle(radius, unitOfmeasure);
                    std::cout
                            <<"ID = "
                            << std::fixed << cir->getId()
                            <<", Type = "
                            << std::fixed << cir->getShapeType()
                            <<", Area  = "
                            << std::fixed << cir->getArea()
                            << std::fixed << cir->getunitOfmeasure()
                            <<std::endl;



            }

        }
        catch (string e)
        {


        }
    }
    return 0;
}

main.h

#include <iostream>
#include <string>
#include "Rectangle.h"
#include "Square.h"
#include "Circle.h"


#ifndef MAIN_H
#define MAIN_H

#ifdef __cplusplus
extern "C" {
#endif


#ifdef __cplusplus
}
#endif

#endif /* MAIN_H */


Circle.cpp

#include <cmath>
#include "Circle.h"
#include <string>
#include "ZeroException.h"
#include "negativeexception.h"

using namespace std;

{
    try
    {
        cout<< "Please enter the diameter: ";
        cin>> diameter;
        if (radius < 0)
        {
            throw NegativeException("Negative number entered.");
            else if (diameter==0)
                throw ZeroException("Zero can't be entered.");
        }
        catch (ZeroException ze)
        {
            diameter = 0;
            cout<< ze.what();
        }
        catch (NegativeException ne)
        {
            diameter = 0;
            cout << ne.what();
        }
    }
}

Circle::Circle(float diameter, string unitOfmeasure) :Shape(CIRCLE, unitOfmeasure){
    this->diameter=diameter;
}

float Circle::getArea(){
    float radius=diameter/2;
    return M_PI *(radius*radius);
}
Circle::~Circle() {
}


Circle.h

#ifndef CIRCLE_H
#define CIRCLE_H
#include "Shape.h"
#include <string>
using namespace std;

class Circle :public Shape{
public:;
    Circle(float diameter, string unitOfmeasure);
       float getArea();
      virtual ~Circle();
private:
    float diameter;
};

#endif /* CIRCLE_H */

NEGATIVEEXCEPTION.h

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

class NegativeException : public std::exception
{
    private:
        std::string m_msg = "negative number entered \n";
    public:
        const char* what() const throw() final override
        {
            return m_msg.c_str();
        }
};

#endif // NEGATIVEEXCEPTION_H_INCLUDED

rectangle.cpp

#include "Rectangle.h"
#include <string>
#include "ZeroException.h"
#include "negativeexception.h"

using namespace std;

{
    try
    {
        cout << "Please enter the length of the rectangle: ";
        cin >> length;
        cout << "Please enter the width of the rectangle: ";
        cin >> width;
        if (length < 0 || width < 0)
        {
            throw NegativeException("Negative number entered.");
        else if (length == 0 || width == 0)
            throw ZeroException("Zero cannot be entered.");
        }
        catch (ZeroException ze)
        {
            length = 0;
            width = 0;
            cout << ze.what();
        }
        catch (NegativeException ne)
        {
            length = 0;
            width = 0;
            cout << ne.what();
        }
    }
}

Rectangle::Rectangle(float w, float h,  string unitOfmeasure):Shape(RECTANGLE, unitOfmeasure) {
    width=w;
    height=h;
}
float Rectangle::getArea(){  // Makes the calculation to get the area.
    return height*width;
}
Rectangle::~Rectangle() {
}
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
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

Rectangle.h

#ifndef RECTANGLE_H
#define RECTANGLE_H
#include "Shape.h"
using namespace std;

class Rectangle : public Shape {
public:
    Rectangle(float width, float height, string unitOfmeasure);
    float getArea();
    virtual ~Rectangle();
private:
    float height;
    float width;
    string unitOfmeasure;
};

#endif /* RECTANGLE_H */

Shape.cpp

#include "Shape.h"

const string Shape::RECTANGLE = "RECTANGLE";
const string Shape::SQUARE = "SQUARE";
const string Shape::CIRCLE ="CIRCLE";

static int sNextId = 10;
Shape::Shape(string shapeType, string unitOfMeasure) {
    this->shapeType=shapeType;
    this->unitOfMeasure = unitOfMeasure;
    id=++sNextId;
}
int Shape::getId(){
    return id;
}
string Shape::getShapeType(){
    return shapeType;
}
string Shape::getunitOfmeasure(){
    return unitOfMeasure;
}


Shape::~Shape() {
}



Shape.h


#include <iostream>
#ifndef SHAPE_H
#define SHAPE_H
#include <string>

using namespace std;
class Shape {

public:
    static const string RECTANGLE;
    static const string SQUARE;
    static const string CIRCLE;

    Shape(string shapeType, string unitOfMeasure);
    int getId();
    string getShapeType();
    string getunitOfmeasure();
    static void setError(string  errorString);

    virtual float getArea()=0;
    virtual ~Shape();
private:
     int id;
     string shapeType;
     string unitOfMeasure;
};

#endif /* SHAPE_H */

Square.cpp

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

using namespace std;

Square::Square(float slength, float swidth, string unitOfmeasure):Shape(SQUARE, unitOfmeasure) {
    this->slength=slength;
    this->swidth=swidth;
}
float Square::getArea(){
    return slength*swidth;
}
Square::~Square() {
}

square.h

#ifndef TRIANGLE_H
#define SQUARE_H
#include <iostream>
#include "Shape.h"


using namespace std;

class Square :public Shape{
public:
    Square(float slength,float swidth, string unitOfmeasure);
     float getArea();
   virtual ~Square();
private:
    float slength;
    float swidth;
    string unitOfmeasure;
};

#endif /*SQUARE_H */


ZEROEXCEPTION.h

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

class ZeroException : public std::exception
{
    private:
        std::string m_msg = "zero cannot be entered \n";
    public:
        const char* what() const throw() final override
        {
            return m_msg.c_str();
        }
};

#endif // ZEROEXCEPTION_H_INCLUDED

May I be frank? Your code seems a copy&paste from different sources.
Is this an assignment?

You could try keeping only the essential: a tiny main(), class Shape and class Rectangle.
Make them work. Try not to have a single warning. When you are ready, add the other classes.

When everything seems fine, you can start playing with exceptions.

Happy coding!

He does seem to be a reincarnation of another user, @Enoizat:

http://www.cplusplus.com/forum/beginner/208406/#msg982032
Wow! What a memory, lastchance, congrats!
Yes this is an assessment and I have to use exceptions. And I am stuck trying to get them to work.
Topic archived. No new replies allowed.