Bernd Huber
January 21st, 2002, 06:48 AM
I wrote that class below with three members. I will stream the object 'MyStr' to
'Cout' by overloading the operator<<. Hopefully somebody can help me why this
source code couldn't work.
//class to stream
class CMyStream
{
private:
long m_lValue;
string m_str;
double m_dValue;
public:
CMyStream(long lValue=0, string str="", double dValue=0.0):m_lValue(lValue), m_str(str), m_dValue(dValue){};
virtual ~CMyStream() {};
long numberLong()const {return m_lValue;};
string numberString()const {return m_str;};
double numberDouble()const {return m_dValue;};
friend ostream& operator<<(ostream& of, const CMyStream& MyStr);
};
//Global declaration of operator<<
ostream& operator<<(ostream& of, const CMyStream& MyStr)
{
of << MyStr.numberLong << '/' << MyStr.numberString << '/' << MyStr.numberDouble;
return of;
}
//main for streamin an object
int main(int argc, char* argv[])
{
long lValue = 91287364;
CMyStream MyStr(123564, "TestText", 56.78);
cout << lValue;//this one works
cout << MyStr;//this won't
return 0;
}
'Cout' by overloading the operator<<. Hopefully somebody can help me why this
source code couldn't work.
//class to stream
class CMyStream
{
private:
long m_lValue;
string m_str;
double m_dValue;
public:
CMyStream(long lValue=0, string str="", double dValue=0.0):m_lValue(lValue), m_str(str), m_dValue(dValue){};
virtual ~CMyStream() {};
long numberLong()const {return m_lValue;};
string numberString()const {return m_str;};
double numberDouble()const {return m_dValue;};
friend ostream& operator<<(ostream& of, const CMyStream& MyStr);
};
//Global declaration of operator<<
ostream& operator<<(ostream& of, const CMyStream& MyStr)
{
of << MyStr.numberLong << '/' << MyStr.numberString << '/' << MyStr.numberDouble;
return of;
}
//main for streamin an object
int main(int argc, char* argv[])
{
long lValue = 91287364;
CMyStream MyStr(123564, "TestText", 56.78);
cout << lValue;//this one works
cout << MyStr;//this won't
return 0;
}