Errors

There is 1 error1 Distance is not defined but why distance has been defined in constructor too
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
  #include<stdexcept>
#include<conio.h>
#include <iostream>
using namespace std;



#include<stdexcept>
#include<conio.h>
#include <iostream>
using namespace std;

class Distance
{
private:
	
public:
	int feet , inches;
	Distance():feet(0),inches(0){}
	Distance ( float ft, double inch): feet (ft) , inches (inch){}
	void getDistance()
	{
		cout<<"Enter feet : "<<endl;
		cin>>feet;
		cout<<"Enter Inches :"<<endl;
		cin>>inches;
	}
	Distance operator +(Distance temp)
	{
		 double f= temp.feet+feet;
		 double i=temp.inches+inches;
		if (inches>=12)
		{
			inches=inches-12;
			feet+=1;
		}
		return Distnace(f,i);
	}
	void display()
	{
		cout<<"FEET:"<<feet<<endl;
		cout<<"Inches:"<<inches<<endl;
	}


};

int main () 
{
Distance d1(3,4),d2,d3;
d2.getDistance();
d3=d1+d2;
d3.display();
}
closed account (o3hC5Di1)
Hi there,

Your problem is on line 37:

return Distnace(f,i);

Note the Distnace typo. Fixing that should solve the problem.

All the best,
NwN
damn thank u and i have another question tell me in return distance does it return d3 value and operator needs one argument does it pass d2 or d1 as argument and shouldnt it take 2 arguments like how it is implemented in function
closed account (o3hC5Di1)
Hi there,

I'm not sure what you mean? It seems like you have strung a few sentences together with the word "and", could you please clarify your question?

All the best,
NwN
in line 28 of the code arguments of the operator are (Distance temp) when operator function was called which object was passed for the function was it object d1 or d2 another thing doesnt it need two argument for the addition of two objects
closed account (o3hC5Di1)
Hi there,

No, you can see that the function accesses the "feet" and "inches" member variables directly.
This is because when you overload an operator as a class member, the current object on which the operator is invoked is used as its left hand argument, to clarify:

1
2
3
Distance A, B, C;

C = A+B;


You could also write this as: A.operator+(B)[.
So B will be "temp" and the member variables of A will be accessed directly in the function.

All the best,
NwN
thank you
Topic archived. No new replies allowed.