Binary Problems

Hello people...I'm having problems getting this program to run...Can anybody help me please...Thank You


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include "Distance.h"


int main()
{ 
	Distance dist1, dist2, dist3;

	int feet = 2000;
	int yards = 2000;
	int miles;
	dist1.Initialize(2, 35, 1);
	dist2 = dist1.ConvertFeet(feet);
	dist3.ConvertYards(yards);

	dist1.PrintDistance();
	dist2.PrintDistance();
	dist3.PrintDistance();

	dist3 = dist1.AddDistance(dist2);
	dist3.PrintDistance();

	return 0;

}


.\Main.cpp(12) : error C2679: binary '=' : no operator found which takes a right-hand operand of type 'int' (or there is no acceptable conversion)
c:\documents and settings\darren\my documents\visual studio 2008\projects\distance\distance\Distance.h(23): could be 'Distance &Distance::operator =(const Distance &)'
while trying to match the argument list '(Distance, int)'
.\Main.cpp(19) : error C2679: binary '=' : no operator found which takes a right-hand operand of type 'int' (or there is no acceptable conversion)
c:\documents and settings\darren\my documents\visual studio 2008\projects\distance\distance\Distance.h(23): could be 'Distance &Distance::operator =(const Distance &)'
while trying to match the argument list '(Distance, int)'
binary '=' : no operator found which takes a right-hand operand of type 'int' (or there is no acceptable conversion)

define this operator, prototype should look like this:
Distance& operator=(int i);

If there are still errors, come back and post Distance.h also.
This is what I am working with at the moment..........

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
#include <iostream>     // This is  Distance.h
#include <string>

using namespace std;

	const int FEET_IN_YARDS = 3;
	const int YARDS_IN_MILES = 1760;

class Distance
{
	int feet;
	int yards;
	int miles;
public:
		int FeetAre()const;
		int YardsAre()const;
		int MilesAre()const;
		int AddDistance(Distance d)const;
		int ConvertFeet(int f);
		int ConvertYards(int y);
		void PrintDistance();
		void Initialize(int f, int y, int m);
};

#include "Distance.h"

int Distance::FeetAre() const
{	return feet;
}
int Distance::YardsAre() const
{	return yards;
}
int Distance::MilesAre()
{	return miles;
}
Distance Distance::AddDistance(Distance d) const// function member of class
{
	Distance sum;
	int f = feet = d.feet;
	sum.feet = f % 3;
	int y = yards = d.yards;
	sum.yards = y % YARDS_IN_MILES;
	sum.miles = miles + d.miles + y / YARDS_IN_MILES;
	return sum;
}
Distance Distance::ConvertYards(int y) 
{	
	Distance con;
	con.feet = 3;
	con.yards = y % YARDS_IN_MILES;
	con.miles = y / YARDS_IN_MILES;
	return con;
}
Distance Distance::ConvertFeet(int f) 
{
	Distance con;
	con.feet = 0 
	con.yards = f % FEET_IN_YARDS;
	con.miles = f / YARDS_IN_YARDS;
	return con;
}
void Distance::PrintDistance() 
{	
	cout << "Total Feet are:  "<< FeetAre();
	<<"\nTotal Yards are:  "<< YardsAre();
	<<"\nTotal Miles are:  "<< MilesAre()<<endl:
}
void Distance::Initialize(int newFeet, int newYards, int newMiles) 
{
	feet = newFeet;
	yards = newYards;
	miles = newMiles;
}
Last edited on
You define Distance class, and the you include Distance.h? What is in Distance.h, is this Distance.h?

Function declarations in the class body doesn't match the definitions, notice the difference between the return values. (Distance --> int?)
Can you please explain....I'm still lost
Ok... I changed the values and it still won't work....Does anybody have a solution for me?????
Change your Distance.h to following. It should work :-)

#ifndef DISTANCE_H_
#define DISTANCE_H_

#include <iostream> // This is Distance.h
#include <string>

using namespace std;

const int FEET_IN_YARDS = 3;
const int YARDS_IN_MILES = 1760;

class Distance
{
int feet;
int yards;
int miles;
public:
int FeetAre()const;
int YardsAre()const;
int MilesAre()const;
Distance AddDistance(Distance d);
Distance ConvertFeet(int f);
Distance ConvertYards(int y);
void PrintDistance();
void Initialize(int f, int y, int m);
};



int Distance::FeetAre() const
{ return feet;
}
int Distance::YardsAre() const
{ return yards;
}
int Distance::MilesAre() const
{ return miles;
}
Distance Distance::AddDistance(Distance d)// function member of class
{
Distance sum;
int f = feet = d.feet;
sum.feet = f % 3;
int y = yards = d.yards;
sum.yards = y % YARDS_IN_MILES;
sum.miles = miles + d.miles + y / YARDS_IN_MILES;
return sum;
}
Distance Distance::ConvertYards(int y)
{
Distance con;
con.feet = 3;
con.yards = y % YARDS_IN_MILES;
con.miles = y / YARDS_IN_MILES;
return con;
}
Distance Distance::ConvertFeet(int f)
{
Distance con;
con.feet = 0;
con.yards = f % FEET_IN_YARDS;
con.miles = f / YARDS_IN_MILES;
return con;
}
void Distance::PrintDistance()
{
cout << "Total Feet are: "<< FeetAre() ;
cout<<"\nTotal Yards are: "<< YardsAre();
cout<<"\nTotal Miles are: "<< MilesAre();
}
void Distance::Initialize(int newFeet, int newYards, int newMiles)
{
feet = newFeet;
yards = newYards;
miles = newMiles;
}


#endif /* DISTANCE_H_ */
I'm sorry but that didn't work either...it said..."fatal error C1070: mismatched #if/#endif pair in file"......but Thanks anyway...Now I learnt a new directive...so that everything is clear...This is the complete program as is......and I still need help........

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
// Header file
#include <iostream>
#include <string>

using namespace std;

	const int FEET_IN_YARDS = 3;
	const int YARDS_IN_MILES = 1760;

class Distance
{
	int feet;
	int yards;
	int miles;
public:
		int FeetAre()const;
		int YardsAre()const;
		int MilesAre()const;
		int AddDistance(Distance d)const;
		int ConvertFeet(int f);
		int ConvertYards(int y);
		void PrintDistance();
		void Initialize(int f, int y, int m);
};
******************************************************************************
//Implementation File
#include "Distance.h"

int Distance::FeetAre() const
{	return feet;
}
int Distance::YardsAre() const
{	return yards;
}
int Distance::MilesAre() 
{	return miles;
}
Distance Distance::AddDistance(Distance d) const // function member of class
{
	Distance sum;
	int f = d.feet;
	sum.feet = f % 3;
	int y = d.yards;
	sum.yards = y % YARDS_IN_MILES;
	sum.miles = miles + d.miles + y / YARDS_IN_MILES;
	return sum;
}
Distance Distance::ConvertYards(int y) 
{	
	Distance con;
	con.feet = 3;
	con.yards = y % YARDS_IN_MILES;
	con.miles = y / YARDS_IN_MILES;
	return con;
}
Distance Distance::ConvertFeet(int f) 
{
	Distance con;
	con.feet = 0; 
	con.yards = f % FEET_IN_YARDS;
	con.miles = f / YARDS_IN_MILES;
	return con;
}
void Distance::PrintDistance() 
{	
	cout << "Total Feet are:  " << FeetAre()
	<<"\nTotal Yards are:  " << YardsAre()
	<<"\nTotal Miles are:  " << MilesAre()<<endl;
}
void Distance::Initialize(int newFeet, int newYards, int newMiles) 
{
	feet = newFeet;
	yards = newYards;
	miles = newMiles;
}
******************************************************************************
//Main
#include "Distance.h"


int main()
{ 
	Distance dist1, dist2, dist3;

	int feet = 2000, yards = 2000, miles;
	dist1.Initialize(2, 35, 1);
	dist2 = dist1.ConvertFeet(feet);
	dist3.ConvertYards(yards);

	dist2.PrintDistance();
	dist3.PrintDistance();
	dist1.PrintDistance();

	dist3 = dist1.AddDistance(dist2);
	dist3.PrintDistance();

	return 0;

}


Topic archived. No new replies allowed.