Exception classes
Sep 13, 2017 at 8:48pm UTC
Hi everybody,
I am now having an issue with the exception classes on my Calculating the area program. It keeps giving me these errors:
On the zeroexceptions.h file:
error: expected class name before '{' token.
error: string does not name a type.
error: expected ')' before '_msg'.
error: 'msg' was not declared in this scope.
The same for negativeexceptions.h file.
for circle.cpp:
error: expected unqualified-id before '{' token
Because it is such a long code I will only be putting in what I think I need help with but I have the entire code so if anyone needs it I will share it with all of you.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
ZeroException.h
#include <exception>
#include <string>
#ifndef ZEROEXCEPTION_H_INCLUDED
#define ZEROEXCEPTION_H_INCLUDED
class ZeroException : public exception
{
private :
string msg;
public :
ZeroException(string _msg)
{
msg = _msg;
}
virtual const char * what() const throw ()
{
return msg.c_str();
}
};
#endif // ZEROEXCEPTION_H_INCLUDED
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
NegativeException.h
#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 // NEGATIVEEXCEPTION_H_INCLUDED
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
circle.cpp
#include "Circle.h"
#include "ZeroException.h"
#include "negativeexception.h"
Circle::Circle(int r)
{
radius = r;
}
{
try
{
cout << "Please enter the radius: " ;
cin >> radius;
if (radius < 0)
throw NegativeException("Negative number entered." );
else if (radius == 0)
throw ZeroException("Zero cannot be entered." );
}
catch (ZeroException ze)
{
radius = 0;
cout << ze.what();
}
catch (NegativeException ne)
{
radius = 0;
cout << ne.what();
}
}
int Circle::getRadius() const
{
return radius;
}
void Circle::setRadius(int r)
{
radius = r;
}
double Circle::getArea()
{
return radius * radius * 3.14;
}
Thank You guys in advance.
Last edited on Sep 13, 2017 at 8:48pm UTC
Sep 13, 2017 at 9:13pm UTC
Your include guards should be the first thing in your header file. And remember that you need to properly scope the item from the std:: namespace.
Sep 13, 2017 at 10:23pm UTC
so you mean like this
1 2
{
class ZeroException : public exception
Now as far as the errors I just have 1 its an unqualified-id before the '{' token.
Last edited on Sep 13, 2017 at 11:09pm UTC
Sep 14, 2017 at 1:27am UTC
Please post your current code along with the complete error message.
Sep 14, 2017 at 2:55am UTC
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
#include <exception>
#include <string>
#ifndef ZEROEXCEPTION_H_INCLUDED
#define ZEROEXCEPTION_H_INCLUDED
{
class ZeroException : public exception
private :
string msg;
public :
ZeroException(string _msg)
{
msg = _msg;
}
virtual const char * what() const throw ()
{
return msg.c_str();
}
};
#endif // ZEROEXCEPTION_H_INCLUDED
error: expected unqualified-id before '{' token.
Sorry, hopefully this clears things up.
Sep 14, 2017 at 4:25am UTC
catch the exception as const reference to base class and polymorphism will print the corresponding what() member function, don't need separate catch-blocks:
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
# include <iostream>
# include <string>
# include <exception>
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();
}
};
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();
}
};
class Circle
{
private :
int m_radius = 0;
public :
Circle ()
{
try
{
std::cout << "enter radius: \n" ;
int radius{};
std::cin >> radius;
if (radius < 0) throw NegativeException();
else if (radius == 0) throw ZeroException();
else m_radius = radius;
}
catch (const std::exception& e )
{
m_radius = 0;
std::cout << e.what();
}
}
void showRadius()const {std::cout << "radius: " << m_radius << "\n" ;}
};
int main()
{
Circle c{};
c.showRadius();
}
also note that using keyword throw for dynamic exception specification is deprecated:
http://en.cppreference.com/w/cpp/language/except_spec
edit:
const reference to base class on line 1
Last edited on Sep 14, 2017 at 10:56am UTC
Sep 14, 2017 at 3:04pm UTC
Is that last one circle.h or circle .cpp. Also if that's the case shouldn't I change square and rectangle as well.
Sep 14, 2017 at 3:29pm UTC
Here's my full code
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 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393
main.cpp
#include <iostream>
#include "Circle.h"
#include "Square.h"
#include "Rectangle.h"
using namespace std;
int main()
{
cout << "Calculating the Area" << endl;
int option = 0;
do
{
cout << "\nPick a shape in which you would like the area of:"
"\n1: Square"
"\n2: Circle"
"\n3: Rectangle"
"\n4: Exit"
"\nPlease enter your choice: " ;
cin >> option;
if (option >= 5 || option <= 0)
{
cout << "\nNumber Not Valid! Please enter a valid number. " << endl;
}
else if (option == 1 || option <=3)
{
cout << "Thank you for the shape." << endl;
}
switch (option)
{
case 1:
{
cout << endl;
cout << "Please enter the length of one side of the square: " << endl;
double length = 0;
cin >> length;
Square square(length);
cout << "The area of the square is: " << square.getArea() << "\n\n" ;
break ;
}
case 2:
{
cout << endl;
cout << "Please enter the radius of the circle: " ;
double radius = 0;
cin >> radius;
Circle circle;
circle.setRadius(radius);
cout << "The area of the circle is: " << circle.getArea() << "\n\n" ;
break ;
}
case 3:
{
cout << endl;
cout << "Please enter the length of one side of the rectangle: " ;
double length = 0;
cin >> length;
Rectangle rectangle;
rectangle.setLength(length);
cout << "Please enter the width of one side of the rectangle: " ;
double width = 0;
cin >> width;
rectangle.setWidth(width);
cout << "The area of the rectangle is: " << rectangle.getArea() << "\n\n" ;
break ;
}
}
}
while (option != 4);
cout << "Bye!" << endl;
}
rectangle.h
#ifndef RECTANGLE_H_INCLUDED
#define RECTANGLE_H_INCLUDED
#include "Shape.h"
class Rectangle : public Shape {
public :
Rectangle (double length = 0, double width = 0);
double getLength() const ;
double getWidth() const ;
void setLength(double length);
void setWidth(double width);
double getArea() override;
private :
double length;
double width;
};
#endif // RECTANGLE_H_INCLUDED
shape.h
#ifndef SHAPE_H_INCLUDED
#define SHAPE_H_INCLUDED
class Shape {
public :
Shape();
virtual double getArea() = 0;
};
#endif // SHAPE_H_INCLUDED
shape.cpp
#include "shape.h"
Shape::Shape()
{}
circle.h
#ifndef CIRCLE_H_INCLUDED
#define CIRCLE_H_INCLUDED
#include "Shape.h"
class Circle : public Shape {
public :
Circle (int radius = 0);
int getRadius() const ;
void setRadius(int radius);
double getArea() override;
private :
int radius;
};
#endif // CIRCLE_H_INCLUDED
square.h
#ifndef SQUARE_H_INCLUDED
#define SQUARE_H_INCLUDED
#include "Shape.h"
class Square : public Shape {
public :
Square (int length = 0);
double getLength() const ;
void setLength(int length_arg);
double getArea() override;
private :
double length;
};
#endif // SQUARE_H_INCLUDED
circle.cpp
#include "Circle.h"
#include "ZeroException.h"
#include "negativeexception.h"
Circle::Circle(int r)
{
radius = r;
}
{
try
{
cout << "Please enter the radius: " ;
cin >> radius;
if (radius < 0)
throw NegativeException("Negative number entered." );
else if (radius == 0)
throw ZeroException("Zero cannot be entered." );
}
catch (ZeroException ze)
{
radius = 0;
cout << ze.what();
}
catch (NegativeException ne)
{
radius = 0;
cout << ne.what();
}
}
int Circle::getRadius() const
{
return radius;
}
void Circle::setRadius(int r)
{
radius = r;
}
double Circle::getArea()
{
return radius * radius * 3.14;
}
rectangle.cpp
#include "Rectangle.h"
#include "ZeroException.h"
#include "negativeexception.h"
Rectangle::Rectangle(double len, double wid)
{
length = len;
width = wid;
}
{
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();
}
}
}
double Rectangle::getLength() const
{
return length;
}
void Rectangle::setLength(double len)
{
length = len;
}
double Rectangle::getWidth() const
{
return width;
}
void Rectangle::setWidth(double wid) {
width = wid;
}
double Rectangle::getArea()
{
return width * length;
}
square.cpp
#include "Square.h"
#include "ZeroException.h"
#include "negativeexception.h"
Square::Square(int len)
{
length = len;
}
double Square::getLength() const
{
return length;
}
{
try
{
cout << "Please enter the length of the square: " ;
cin >> length;
if (length < 0)
throw NegativeException("Negative number entered." );
else if (length == 0)
throw ZeroException("Zero cannot be entered." );
}
catch (ZeroException ze)
{
length = 0;
cout << ze.what();
}
catch (NegativeException ne)
{
length = 0;
cout << ne.what();
}
}
void Square::setLength(int length_arg)
{
length = length_arg;
}
double Square::getArea()
{
return length * length;
}
zeroexception.h
#include "Square.h"
#include "ZeroException.h"
#include "negativeexception.h"
Square::Square(int len)
{
length = len;
}
double Square::getLength() const
{
return length;
}
{
try
{
cout << "Please enter the length of the square: " ;
cin >> length;
if (length < 0)
throw NegativeException("Negative number entered." );
else if (length == 0)
throw ZeroException("Zero cannot be entered." );
}
catch (ZeroException ze)
{
length = 0;
cout << ze.what();
}
catch (NegativeException ne)
{
length = 0;
cout << ne.what();
}
}
void Square::setLength(int length_arg)
{
length = length_arg;
}
double Square::getArea()
{
return length * length;
}
negativeexception.h
#include "Square.h"
#include "ZeroException.h"
#include "negativeexception.h"
Square::Square(int len)
{
length = len;
}
double Square::getLength() const
{
return length;
}
{
try
{
cout << "Please enter the length of the square: " ;
cin >> length;
if (length < 0)
throw NegativeException("Negative number entered." );
else if (length == 0)
throw ZeroException("Zero cannot be entered." );
}
catch (ZeroException ze)
{
length = 0;
cout << ze.what();
}
catch (NegativeException ne)
{
length = 0;
cout << ne.what();
}
}
void Square::setLength(int length_arg)
{
length = length_arg;
}
double Square::getArea()
{
return length * length;
}
Thank you guys
Sep 14, 2017 at 5:43pm UTC
Your indentation is horrible, if you used a consistent indent style your problems would probably be easier to see.
Look at this snippet:
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
#include "Rectangle.h"
#include "ZeroException.h"
#include "negativeexception.h"
Rectangle::Rectangle(double len, double wid)
{
length = len;
width = wid;
}
{
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();
}
}
}
double Rectangle::getLength() const
{
return length;
}
void Rectangle::setLength(double len)
{
length = len;
}
double Rectangle::getWidth() const
{
return width;
}
void Rectangle::setWidth(double wid)
{
width = wid;
}
double Rectangle::getArea()
{
return width * length;
}
Do you see anything out of place? (Hint look at line 11 through line 38.)
Sep 14, 2017 at 6:30pm UTC
I'm sorry I was just following what the program let me do.
Sep 15, 2017 at 3:42pm UTC
Hello Everybody,
Ok Now the only problem I have is it keeps throwing me this error.
error:expected unqualified-id before '{' token.
It is talking about the { before try. If you guys could help I would be very grateful.
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
circle.cpp
#include "Circle.h"
#include "ZeroException.h"
#include "negativeexception.h"
Circle::Circle(int r)
{
radius = r;
}
int Circle::getRadius() const
{
return radius;
}
{
try
{
cout << "Please enter the radius: " ;
cin >> radius;
if (radius < 0)
throw NegativeException("Negative number entered." );
else if (radius == 0)
throw ZeroException("Zero cannot be entered." );
}
catch (ZeroException ze)
{
radius = 0;
cout << ze.what();
}
catch (NegativeException ne)
{
radius = 0;
cout << ne.what();
}
}
void Circle::setRadius(int r)
{
radius = r;
}
double Circle::getArea()
{
return radius * radius * 3.14;
}
Last edited on Sep 15, 2017 at 3:42pm UTC
Sep 15, 2017 at 4:01pm UTC
Lines 16 - 36 aren't part of any function. That's not legal C++.
Last edited on Sep 15, 2017 at 4:01pm UTC
Topic archived. No new replies allowed.