Click here to Skip to main content
15,903,854 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
#include<vector>
#include<iostream>

class mytype
{
private:

	int cor[2];

public:

	mytype();
	mytype(int x , int y);
	
	~mytype(){}

	mytype (const mytype & m);
	mytype & operator = (const mytype & m);

	friend std::ostream & operator << (std::ostream os , const mytype & m);

};

mytype::mytype()
{

	cor[0]=0;
	cor[1]=0;

}

mytype::mytype(int x , int y)
{

	cor[0]=x;
	cor[1]=y;

}

mytype::mytype(const mytype & m)
{

	cor[0]=m.cor[0];
	cor[1]=m.cor[1];

}

mytype & mytype::operator = (const mytype & m)
{

	cor[0]=m.cor[0];
	cor[1]=m.cor[1];
	return * this;

}

std::ostream & operator << (std::ostream & os , const mytype & m)
{

	os << m.cor[0] << "," << m.cor[1];
	return os;

}



I overload "<<" with a friend fun in mytype class.But when I define it , the compiler say that I can't access the member of m. I don't know why! Who can tell me what's wrong with it?
Posted

It should be:
C++
ostream& operator<<(ostream& os, const myType& dt);


Please see: http://msdn.microsoft.com/en-us/library/1z2f6c2k%28v=vs.110%29.aspx[^].

—SA
 
Share this answer
 
The friend declaration doesn't match the function definition, there's a missing &
C++
friend std::ostream & operator << (std::ostream os , const mytype & m);

C++
std::ostream & operator << (std::ostream & os , const mytype & m)
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900