operator overloading

Ok so I am pretty new to programming and as you will see It does not agree with me too well. I have written this code that is supposed to ooutput the information to a console but I keep getting this error about no operator found which takes a right-hand operand of type 'RacingData'. I know what the problem is: I never defined the operator<< that would take RacingData as a parameter. I was told to fix it with something along these lines
1
2
3
4
5
6
7
8
ostream& operator<<(ostream& os, RacingData& data) {
   // Do whatever you need to print 'data' to 'os'. For example:
   os << data.getName() << endl;

    return os;
}

but when I do that, I get like 5 other errors. So my question is how do I implement this into my code so that it will work propeerly?
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
#include <iostream>
#include <string>

using namespace std;

class RacingData
{
public:
	void setData(string, int, int, int);
	string getName(string);
	int getScore(int);
	int getMinute(int);
	int getSecond(int);
	void printData();
	RacingData operator+(RacingData&);
	RacingData(string, int, int, int);
	RacingData();
private:
	string name;
	int score;
	int minute;
	int second;
};

string RacingData::getName(string name)
{
	return name;
}
int RacingData::getScore(int xScore)
{
	return xScore;
}
int RacingData::getMinute(int xMinute)
{
	return xMinute;
}
int RacingData::getSecond(int xSecond)
{
	return xSecond;
}

void RacingData::setData(string name, int xScore, int xMinute, int xSecond)
{
	this->name = name;
	score = xScore;
	minute = xMinute;
	second = xSecond;
}

void RacingData::printData()
{
	cout << "Name:\t\t\t" << "Score:\t\t" << "Minutes:\t" << "Seconds:" << endl;
	cout << name << "\t\t" << score << "\t\t" << minute << "\t\t" << second << endl;
}

RacingData::RacingData(string name, int xScore, int xMinute, int xSecond)
{
	name = name;
	score = xScore;
	minute = xMinute;
	second = xSecond;
}
RacingData::RacingData()
{

}

RacingData RacingData::operator+(RacingData& racingdata)
{
	RacingData tempData;

	tempData.minute = minute + racingdata.minute;
	tempData.second = second + racingdata.second;
	tempData.score = score + racingdata.score;

	return tempData;
}


int main()
{
	RacingData racingDataT1;
	RacingData racingDataT2;

	racingDataT1.setData("Danica Patrick", 185, 11, 20);
	cout << "Testing using setData and printData...\n" << endl;
	racingDataT1.printData();
	racingDataT1.printData();
	cout << "\n" << endl;

	string currentName = racingDataT2.getName("Danica Patrick");
	int currentScore = racingDataT2.getScore(185);
	int currentMinute = racingDataT2.getMinute(11);
	int currentSecond = racingDataT2.getSecond(20);
	cout << "\ntesting using getData...\n" << endl;
	cout << "Name:\t\t\t" << "Score:\t\t" << "Minutes:\t\t" << "Seconds: " << endl;
	cout << currentName << "\t\t" << currentScore << "\t\t" << currentMinute << "\t\t\t" << currentSecond << "\n" << endl;

	RacingData racingDataD1;
	racingDataD1.setData("Danica Patrick", 185, 11, 20);
	RacingData racingDataD2;
	racingDataD2.setData("Danica Patrick", 103, 11, 30);
	RacingData racingDataD3;
	racingDataD3.setData("Danica Patrick", 73, 12, 40);
	RacingData racingDataDT;
	racingDataDT = racingDataD1 + racingDataD2 + racingDataD3;
	cout << "Danica's total:\n" << endl;
	

	RacingData racingDataJ1;
	racingDataJ1.setData("Jeff Gordon", 155, 10, 10);
	RacingData racingDataJ2;
	racingDataJ2.setData("Jeff Gordon", 127, 11, 15);
	RacingData racingDataJ3;
	racingDataJ3.setData("Jeff Gordon", 34, 12, 35);

	RacingData racingDataJT;
	racingDataJT = racingDataJ1 + racingDataJ2 + racingDataJ3;
	cout << "Jeff's total:\n" << endl;
	cout << racingDataJT << endl;  // here is the error line

	return 0;
};
Last edited on
The function should be deined as:
 
ostream& operator<<(ostream& os, const RacingData& data)


That aside, what are the other errors? If you're not specific, we can't help.
Topic archived. No new replies allowed.