Unhandled Exception Problem

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
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;
};


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
#include "Fraction.h"
#include "Circle.h"
#include <iostream>
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
1
2
 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
i've added the code
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?
1
2
3
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.
1
2
3
4
5
6
7
8
9
10
11
12
class Circle{
//...
   Fraction radius;
};

Circle::Circle(const Fraction &r):
   radius(r)
{}

int main(){
   Circle bar( Fraction(3,14) );
}
Topic archived. No new replies allowed.