Avatar of Unimatrix_001
Unimatrix_001Flag for United Kingdom of Great Britain and Northern Ireland

asked on 

Overloading <<

Hi.

See attached code snippet. When I try to use MC as cout<<object<<endl; I get a:

binary '<<' : no operator found which takes a right-hand operand of type 'MC'

Reasons and a nice fix would be appreciated. I can get around the problem by making the data protected and making the method a friend, but ideally I want the overloaded function as part of the class.

Thanks,
Uni
class MC{
public:
	ostream& operator<<(ostream &aOs);
private:
	int data;
};
 
 
ostream& MC::operator<<(ostream &aOs){
	aOS<<data;
	return aOS;
}

Open in new window

C++

Avatar of undefined
Last Comment
Unimatrix_001
ASKER CERTIFIED SOLUTION
Avatar of Jaime Olivares
Jaime Olivares
Flag of Peru image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Avatar of Unimatrix_001
Unimatrix_001
Flag of United Kingdom of Great Britain and Northern Ireland image

ASKER

Very well...
C++
C++

C++ is an intermediate-level general-purpose programming language, not to be confused with C or C#. It was developed as a set of extensions to the C programming language to improve type-safety and add support for automatic resource management, object-orientation, generic programming, and exception handling, among other features.

58K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo