cout not working with CString  
Author Message
LachlanAtWork





PostPosted: Visual C++ General, cout not working with CString Top

I am using Visual C++.NET V7.0.9955

When I use the following code

CString name = "John";

cout << name;

Rather than getting the name on the counsole I get a hex value, probable the address of the location of name. Of course if i change the code to cout << *name; I get the first letter of name.

Why wont cout work for CString. I thought it should.

Thanks Lachlan.



Visual C++11  
 
 
SvenC





PostPosted: Visual C++ General, cout not working with CString Top

I guess you are struggeling on characters sets. By default VC2005 uses unicode. So you should use wcout.

What you see is the address of name. You can call the operator which returns the address of the character array: (LPCTSTR)name.

You can also add an operator for ostream and wostream:

std::wostream& operator<<(std::wostream& wostr, const CString& s)
{
wostr << (LPCTSTR)s;
return wostr;
}

Do the same for ostream if you do multi byte char set builds.

--
SvenC