Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Problem displaying string

Status
Not open for further replies.

Ciralia

Programmer
Oct 22, 2002
51
US
I have 2 string variables within a struct. Later in a function of the struct, I define the variables such as...

string S1;

//within function
S1 = "Hello";

Then when I try to cout the value of the strings later like this...

cout << S1;

I get an error about the << operator not being defined. Anyone know how to solve this? I did include iostream, and string also.
 
The string class does not have a << operator. To get this to work (without explaining why), try using:

cout << S1.c_str();

The class &quot;string&quot; is a type of the class &quot;basic_string&quot;. In VC++ help files (at least for 6.0) look in the Index under &quot;string&quot;, then select the option of &quot;C/C++ Languages and C++ Libraries&quot;. It should give a description there of how it's defined, along with links to basic_string and it's header file with all the operators you can use for it.

If you want a more robust string class and you are already using MFC, try using the CString class. It's much more user friendly and supports &quot;cout << S1;&quot; if S1 is of type CString.

Enjoy,

Kelek
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top