Overloading operator and ostream

In my program I'm trying to override == operator and also stream operator <<. I have some errors in these functions. Please help to fix my code.

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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
 #include <iostream>
#include <string>
#include <iomanip>

using namespace std;

class Vehicle
{
public:
	Vehicle(double price = 0, int mpg = 0);
	void setPrice(double price);
	void setMpg(int mpg);
	double getPrice() const;
	int getMpg() const;
	void print() const;

private:
	double price;
	int mpg;
};

class Loan
{
public:
	Loan(string bank = "", double amount = 0);
	void setBank(string bank);
	void setAmount(double amount);
	string getBank() const;
	double getAmount() const;
	void print() const;

private:
	string bank;
	double amount;
};

class Car : public Vehicle
{
	friend ostream& operator<<(ostream&, const Car&);
public:
	Car(int nbrOfLoans = 2,

		double price = 0,
		int mpg = 0,

		string bank = "",
		double amount = 0,

		string name = ""
	);

	void setName(string name);
	string getName() const;
	void setLoan();
	Loan* getpLoan() const;
	void print() const;
	~Car();
	bool operator== (const Car&) const;

private:
	string name;
	Loan *pLoan;
	int nbrOfLoans;
};

Vehicle::Vehicle(double price, int mpg)
{
	Vehicle::price = price;
	Vehicle::mpg = mpg;
}

void Vehicle::setPrice(double price)
{
	Vehicle::price = price;
}

void Vehicle::setMpg(int mpg)
{
	Vehicle::mpg = mpg;
}

double Vehicle::getPrice() const
{
	return price;
}

int Vehicle::getMpg() const
{
	return mpg;
}

void Vehicle::print() const
{
	cout << "Price: " << price << endl;
	cout << "MPG: " << mpg << endl;
}


Loan::Loan(string bank, double amount)
{
	Loan::bank = bank;
	Loan::amount = amount;
}
void Loan::setBank(string bank)
{
	Loan::bank = bank;
}
void Loan::setAmount(double amount)
{
	Loan::amount = amount;
}
string Loan::getBank() const
{
	return bank;
}
double Loan::getAmount() const
{
	return amount;
}
void Loan::print() const
{
	cout << "Bank: " << bank << endl;
	cout << "Amount: " << amount << endl;
}


Car::Car(int nbrOfLoans,
	double price, int mpg,
	string bank, double amount,
	string name)
	:Vehicle(price, mpg)
{
	Car::name = name;
	Car::nbrOfLoans = nbrOfLoans;
	Car::pLoan = new Loan[nbrOfLoans];
}

Car::~Car()
{
	delete[]pLoan;
}

void Car::setName(string name)
{
	Car::name = name;
}
string Car::getName() const
{
	return name;
}
void Car::setLoan()
{
	double amount;
	string bank;

	for (int i = 0; i < nbrOfLoans; i++)

	{
		cout << "Enter bank and amount for loan " << i + 1 << ": ";
		cin >> bank >> amount;
		pLoan[i].setBank(bank);
		pLoan[i].setAmount(amount);
	}
}

Loan* Car::getpLoan() const
{
	return pLoan;
}

void Car::print() const
{
	Vehicle::print();

	for (int i = 0; i < nbrOfLoans; i++)
	{
		cout << endl << "Loan " << i + 1 << ": " << endl;
		pLoan[i].print();
		cout << endl;
	}

	cout << "Name: " << name << endl << endl;

	cout << fixed << showpoint << setprecision(1) << endl;
}

ostream& operator<<(ostream& os, const Car& car)
{
	Loan* pLoan = car.getpLoan();
	os << "Car name: " << car.name << endl
		<< "MPG: " << car.getMpg() << endl
		<< "Bank: " << pLoan[0].getBank();
	return os;
}

bool Car::operator== (const Car& car) const
{
	if (
		name = car.name
		&& pLoan[0].getAmount() == car.pLoan[0].getAmount
		&& pLoan[0].getBank() == car.pLoan[0].getBank
		&& getMpg() == car.getMpg()
		&& getPrice == car.getPrice
		)

		return true;
}


Severity Code Description Project File Line Suppression State
Error C3867 'Loan::getAmount': non-standard syntax; use '&' to create a pointer to member 205
Error C2446 '==': no conversion from 'double (__thiscall Loan::* )(void) const' to 'double' 205
Error C3867 'Loan::getBank': non-standard syntax; use '&' to create a pointer to member 206
Error C2679 binary '==': no operator found which takes a right-hand operand of type 'overloaded-function' (or there is no acceptable conversion) 206
Error C3867 'Vehicle::getPrice': non-standard syntax; use '&' to create a pointer to member 208
closed account (D80DSL3A)
The errors about non standard syntax are from how you're qualifying data members. You caused a name shadowing problem by naming the function parameters the same as the class data member names. eg.
1
2
3
4
5
// example of poor coding practice causing problems
void Loan::setBank(string bank) 
{
	bank = bank;// ambiguous.Which is supposed to be which here?
}

Qualifying the data member should fix this, but Loan::bank would refer to a static class data member (your class has none), not a regular one.
Usethis->bank instead.
1
2
3
4
void Loan::setBank(string bank) 
{
	this->bank = bank;// fixed
}

Or better yet, dodge the issue with a different argument name:
1
2
3
4
void Loan::setBank(string Bank) 
{
	bank = Bank;// ambiguity be gone!
}

The other errors re no overload for ==...
maybe caused by lines 200, 201 where function calls are incomplete, eg:
car.pLoan[0].getAmount
should be
car.pLoan[0].getAmount()
Thank you! it was very helpful and took off all the errors. Everything is fine except in my predefined variable (car1), I cannot get bank and amount in print and I just get name, MPG and price. Other variable (car2) is fine. Where is the 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
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
#include <iostream>
#include <string>
#include <iomanip>

using namespace std;

class Vehicle
{
public:
	Vehicle(double Price = 0, int Mpg = 0);
	void setPrice(double Price);
	void setMpg(int Mpg);
	double getPrice() const;
	int getMpg() const;
	void print() const;

private:
	double price;
	int mpg;
};

class Loan
{
public:
	Loan(string Bank = "", double Amount = 0);
	void setBank(string Bank);
	void setAmount(double Amount);
	string getBank() const;
	double getAmount() const;
	void print() const;

private:
	string bank;
	double amount;
};

class Car : public Vehicle
{
	friend ostream& operator<<(ostream&, const Car&);
public:
	Car(int nbrOfLoans = 2,

		double Price = 0,
		int Mpg = 0,

		string Bank = "",
		double Amount = 0,

		string Name = ""
	);

	void setName(string Name);
	string getName() const;
	void setLoan();
	Loan* getpLoan() const;
	void print() const;
	~Car();
	bool operator== (const Car&) const;

private:
	string name;
	Loan *pLoan;
	int nbrOfLoans;
};

Vehicle::Vehicle(double Price, int Mpg)
{
	price = Price;
	mpg = Mpg;
}

void Vehicle::setPrice(double Price)
{
	price = Price;
}

void Vehicle::setMpg(int Mpg)
{
	mpg = Mpg;
}

double Vehicle::getPrice() const
{
	return price;
}

int Vehicle::getMpg() const
{
	return mpg;
}

void Vehicle::print() const
{
	cout << "Price: " << price << endl;
	cout << "MPG: " << mpg << endl;
}


Loan::Loan(string Bank, double Amount)
{
	bank = Bank;
	amount = Amount;
}
void Loan::setBank(string Bank)
{
	bank = Bank;
}
void Loan::setAmount(double Amount)
{
	amount = Amount;
}
string Loan::getBank() const
{
	return bank;
}
double Loan::getAmount() const
{
	return amount;
}
void Loan::print() const
{
	cout << "Bank: " << bank << endl;
	cout << "Amount: " << amount << endl;
}


Car::Car(int nbrOfLoans,
	double Price, int Mpg,
	string Bank, double Amount,
	string Name)
	:Vehicle(Price, Mpg)
{
	name = Name;
	Car::nbrOfLoans = nbrOfLoans;
	pLoan = new Loan[nbrOfLoans];
}

Car::~Car()
{
	delete[]pLoan;
}

void Car::setName(string Name)
{
	name = Name;
}
string Car::getName() const
{
	return name;
}
void Car::setLoan()
{
	double Amount;
	string Bank;

	for (int i = 0; i < nbrOfLoans; i++)

	{
		cout << "Enter bank and amount for loan " << i + 1 << ": ";
		cin >> Bank >> Amount;
		pLoan[i].setBank(Bank);
		pLoan[i].setAmount(Amount);
	}
}

Loan* Car::getpLoan() const
{
	return pLoan;
}

void Car::print() const
{
	Vehicle::print();

	for (int i = 0; i < nbrOfLoans; i++)
	{
		cout << endl << "Loan " << i + 1 << ": " << endl;
		pLoan[i].print();
		cout << endl;
	}

	cout << "Name: " << name << endl << endl;

	cout << fixed << showpoint << setprecision(1) << endl;
}

ostream& operator<<(ostream& os, const Car& car)
{
	Loan* pLoan = car.getpLoan();
	os << "Car name: " << car.name << endl
		<< "MPG: " << car.getMpg() << endl
		<< "Bank: " << pLoan[0].getBank();
	return os;
}

bool Car::operator== (const Car& car) const
{
	if (
		name == car.name
		&& pLoan[0].getAmount() == car.pLoan[0].getAmount()
		&& pLoan[0].getBank() == car.pLoan[0].getBank()
		&& getMpg() == car.getMpg()
		&& getPrice() == car.getPrice()
		)

		return true;
}

int main()
{
	string name, bank;
	double price, amount;
	int mpg;

	Car car1(1, 24800.0, 22, "Citi", 21600, "Mustang");
	Car car2;
	Car *pCar1;
	Car *pCar2;

	pCar1 = &car1;
	pCar2 = &car2;

	pCar1->print();
	cout << endl;

	cout << "Enter name: ";
	cin >> name;
	cout << "Enter price: ";
	cin >> price;
	cout << "Enter MPG: ";
	cin >> mpg;

	pCar2->setLoan();
	pCar2->setPrice(price);
	pCar2->setMpg(mpg);
	pCar2->setName(name);

	pCar2->print();

	system("pause");
	return 0;
}
closed account (D80DSL3A)
I think you need to call setLoan() for each of the loans created at line 135 in Car constructor.
Also line 134 an old issue?
134
135
136
Car::nbrOfLoans = nbrOfLoans;// No error? qualify as this->nbrOfLoans
pLoan = new Loan[nbrOfLoans];// default constructed
// loop and call setLoan() here 

Glad that last post cleared all errors.
edit: An array of loans for 1 car? Sounds like a finance nightmare.
Last edited on
When I try to call setLoan() it says class "Loan" has no member "setLoan"

1
2
3
	Car::pLoan = new Loan[nbrOfLoans];
	for (int i = 0; i < nbrOfLoans; i++)
		pLoan[i].setLoan();
closed account (D80DSL3A)
Right. I guess it's a Car class member function, not a Loan class function.
I assumed wrong about that.
LOOK IT UP! In your own code, line 151 answers questions re. the setLoan function.
I need more help, I don't get it. Sorry!
closed account (D80DSL3A)
It's ok. With some sleep now maybe I can get these crucial lines in the Car constructor right.
I see you chose to Capitalize argument names to distinguish them from the Car data members, but you missed 1 or 2, so need this->nbrOfLoans in:
134
135
136
137
this->nbrOfLoans = nbrOfLoans;
pLoan = new Loan[nbrOfLoans];// default constructed
// call setLoan(). It loops through all loans
setLoan();

OK, it solved half of the problem, but for my predefined car1 object, it asks foe bank and amount which I don't want to. Please check my main function 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
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
#include <iostream>
#include <string>
#include <iomanip>

using namespace std;

class Vehicle
{
public:
	Vehicle(double myPrice = 0, int myMpg = 0);
	void setPrice(double myPrice);
	void setMpg(int myMpg);
	double getPrice() const;
	int getMpg() const;
	void print() const;

private:
	double price;
	int mpg;
};

class Loan
{
public:
	Loan(string myBank = "", double myAmount = 0);
	void setBank(string myBank);
	void setAmount(double myAmount);
	string getBank() const;
	double getAmount() const;
	void print() const;

private:
	string bank;
	double amount;
};

class Car : public Vehicle
{
	friend ostream& operator<<(ostream&, const Car&);
public:
	Car(int nol = 1,

		double myPrice = 0,
		int myMpg = 0,

		string myBank = "",
		double myAmount = 0,

		string myName = ""
	);

	void setName(string myName);
	string getName() const;
	void setLoan();
	Loan* getpLoan() const;
	void print() const;
	~Car();
	bool operator== (const Car&) const;

private:
	string name;
	Loan *pLoan;
	int nbrOfLoans;
};

Vehicle::Vehicle(double myPrice, int myMpg)
{
	price = myPrice;
	mpg = myMpg;
}

void Vehicle::setPrice(double myPrice)
{
	price = myPrice;
}

void Vehicle::setMpg(int myMpg)
{
	mpg = myMpg;
}

double Vehicle::getPrice() const
{
	return price;
}

int Vehicle::getMpg() const
{
	return mpg;
}

void Vehicle::print() const
{
	cout << "Price: " << price << endl;
	cout << "MPG: " << mpg ;
}


Loan::Loan(string myBank, double myAmount)
{
	bank = myBank;
	amount = myAmount;
}
void Loan::setBank(string myBank)
{
	bank = myBank;
}
void Loan::setAmount(double myAmount)
{
	amount = myAmount;
}
string Loan::getBank() const
{
	return bank;
}
double Loan::getAmount() const
{
	return amount;
}
void Loan::print() const
{
	cout << "Bank: " << bank << endl;
	cout << "Amount: " << amount;
}


Car::Car(int nol,
	double myPrice, int myMpg,
	string myBank, double myAmount,
	string myName)
	:Vehicle(myPrice, myMpg)
{
	name = myName;
	nbrOfLoans = nol;
	pLoan = new Loan[nbrOfLoans];
	setLoan();
}

Car::~Car()
{
	delete[]pLoan;
}

void Car::setName(string myName)
{
	name = myName;
}
string Car::getName() const
{
	return name;
}
void Car::setLoan()
{
	double myAmount;
	string myBank;

	for (int i = 0; i < nbrOfLoans; i++)
	{
		cout << "Enter bank and amount for loan " << i + 1 << ": ";
		cin >> myBank >> myAmount;
		pLoan[i].setBank(myBank);
		pLoan[i].setAmount(myAmount);
	}
}

Loan* Car::getpLoan() const
{
	return pLoan;
}

void Car::print() const
{
	Vehicle::print();

	for (int i = 0; i < nbrOfLoans; i++)
	{
		cout << endl << "Loan " << i + 1 << ": " << endl;
		pLoan[i].print();
		cout << endl;
	}

	cout << "Name: " << name << endl << endl;

	cout << fixed << showpoint << setprecision(1) << endl;
}

ostream& operator<<(ostream& os, const Car& car)
{
	Loan* pLoan = car.getpLoan();
	os << "Price: " << car.getPrice() << endl
		<< "MPG: " << car.getMpg() << endl
		<< "Bank: " << pLoan[0].getBank() << endl
		<< "Amount: " << pLoan[0].getAmount() << endl
		<< "Car name: " << car.name << endl;
		
		
	return os;
}

bool Car::operator== (const Car& car) const
{
	if (
		name == car.name
		&& pLoan[0].getAmount() == car.pLoan[0].getAmount()
		&& pLoan[0].getBank() == car.pLoan[0].getBank()
		&& getMpg() == car.getMpg()
		&& getPrice() == car.getPrice()
		)

		return true;
}

int main()
{
	string name, bank;
	double price, amount;
	int mpg;

	Car car1(1, 24800.0, 22, "Citi", 21600, "Mustang");
	Car car2;
	Car *pCar1;
	Car *pCar2;

	pCar1 = &car1;
	pCar2 = &car2;

	cout << *pCar1 <<endl;
	cout << endl;

	cout << "Enter name: ";
	cin >> name;
	cout << "Enter price: ";
	cin >> price;
	cout << "Enter MPG: ";
	cin >> mpg;

	pCar2->setLoan();
	pCar2->setPrice(price);
	pCar2->setMpg(mpg);
	pCar2->setName(name);

	cout << *pCar2 << endl;

	cout << "Are this cars similar?" << endl;
	if (*pCar1 == *pCar2)
		cout << "Cars are similar." << endl;
	else
		cout << "Cars are different." << endl;

	system("pause");
	return 0;
}
Topic archived. No new replies allowed.