operator char*() not working?
Hi All,
I'm playing with a custom string class.
//////////////////////////////////// in mystring.h
class mystring {
char* p;
public:
mystring(): p(0) {}
mystring(char*);
~mystring();
operator char*() { return p; } // operator char*() defined here
friend ostream& operator<< (ostream&, const mystring&);
};
//////////////////////////////////// in mystring.cpp
ostream& operator<< (ostream& os, const mystring& str)
{
os << (char*)str; // ERROR REPORTED HERE – compiler can't find operator char*()
return os;
}
//////////////////////////////////// in main.cpp
void main()
{
mystring hello = "Hello World!";
cout <<hello;
}
I get the following:
error C2440: 'type cast' : cannot convert from 'const class mystring' to 'char *'
No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called
WHAT? Operator char*() is defined right there! What am I doing wrong?