### Unhandled Exception Problem

 ``1234567891011121314151617181920212223242526`` ``````class Fraction { public: Fraction(); Fraction(int); Fraction(int, int); ~Fraction(); void setNum(int); void setDenom(int); int getNum(void) const; int getDenom(void) const; void print(void); private: int num; int denom; }; class Circle { public: Circle(); Circle(Fraction& arg1); friend ostream& operator<<(ostream&, Circle&); ~Circle(); void print(void); protected: Fraction *radius; };``````

 ``1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071`` ``````#include "Fraction.h" #include "Circle.h" #include using namespace std; Fraction::Fraction() { num = 0; denom = 1; } Fraction::Fraction(int n) { num = n; denom = 1; } Fraction::Fraction(int n, int d) { num = n; denom = d; } void Fraction::setNum(int val) { num = val; return; } void Fraction::setDenom(int val) { denom = val; return; } int Fraction::getNum() const { return num; } int FractionL::getDenom() const { return denom; } Fraction& Fraction::operator=(const Fraction& arg) { num = arg.num; denom = arg.denom; return *this; } void Fraction::print() { cout << "\n The Length is : " << num << endl; cout << " The Width is : " << denom << endl; cout << "\n\n"; } ostream& operator<<(ostream& out, const Fraction& frac) { out << "\n\tNumerator : " << frac.num << "\n\tDenominator : " << frac.denom << endl; return out; } Fraction::~Fraction() { } //Circle Circle::Circle() { radius->setNum(1); radius->setDenom(1); } Circle::Circle(Fraction& arg1) { radius->setNum(arg1.getNum()); radius->setDenom(arg1.getDenom()); } void Circle::print() { cout << "The radius of the Circle is : " << radius->getNum() << ", " << radius->getDenom() << endl; } Circle::~Circle() { delete this->radius; } ``````

The problem in main
 ``12`` `````` Fraction* fPtr = new Fraction(4, 1); Circle* cPtrA = new Circle(*fPtr);``````

Both of these are classes.
First i use the copy constructor to set Numerator of classFraction to 4 and Denominator to 1.

Then i want to pass that (4, 1) into my classCircle but i get this error of unhandled exception through debugger.

Last edited on
Last edited on
In your Circle constructor you are not creating your radius Fraction, so it will be a null pointer and when you do `radius->setNum(1);`, you are dereferencing nullptr which is instant crash.
Last edited on
How to i create radius fraction?
 How to i create radius fraction?
Just like you did in main(). BTW, why you need it to be pointer? Why not just have "radius" member of type Fraction?
earthdeath wrote:
Hi, so do i put this in circle default constructor?
 ``123`` ``````Circle::Circle() { radius = new Fraction; }``````

I still get run time error.
I use pointer because we have to practice different member data.

Please do not move discussion into private messages.

It might be also some parts you didn't show which causes problems. Parts you sho shouldn't even compile (you are missing declaration of << operator for Fraction as friend for example.)
Sorry, it's a long code so i cut it down.
The problem is not the <<
I accidentally took in out in here.
 ``123456789101112`` ``````class Circle{ //... Fraction radius; }; Circle::Circle(const Fraction &r): radius(r) {} int main(){ Circle bar( Fraction(3,14) ); }``````
Topic archived. No new replies allowed.