No Operator matches these operands

Hi there,

I am currently revising for a C++ exam at university, and I got a question which is to do with Functions and Classes.

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
#include "stdafx.h"
#include <iostream>
#include <string>
using namespace std;

class Car
{
public:
	Car()
	{
		driverName = "The Stig";
	}

	void setDriver(string newDriver)
	{
		driverName = newDriver;
	}
private:
	string driverName;
};


int main()
{
	Car car1;
	car1.setDriver("George");
	cout << car1;
	return 0;
}


In the cout << car1; the << are underlined red and just says "No operator "<<" matches these operands. Operand types are: std::ostream << Car"

Taking the cout statement out, the code complies with no errors. Just adding that in throw's up that error.
Last edited on
The compiler can't find an overloaded output operator<< that is needed to print your class.
Here's a stub for you to fill out:

1
2
3
4
5
std::ostream& operator << ( std::ostream& outs, const Car& car )
{
  outs << car.getDriverName() or something. You may want to make this function a friend to Car.
  return outs;
}
Topic archived. No new replies allowed.