• ### Announcements

#### Archived

This topic is now archived and is closed to further replies.

# 'Cout'ing a string requires you to include "string"?

## Recommended Posts

You don't have to inlude "string" to make an std::string. But when I don't include "string", I get this error: "c:\Projects\string test\Main.cpp(8): error C2679: binary '<<' : no operator found which takes a right-hand operand of type 'std::string' (or there is no acceptable conversion) which is pointing the the line a try to 'cout' a string. Why does including a seperate file help? EDIT: Brackets were eaten. [edited by - PlayGGY on March 18, 2004 12:15:42 PM] [edited by - PlayGGY on March 18, 2004 12:16:16 PM] [edited by - PlayGGY on March 18, 2004 12:16:28 PM]

##### Share on other sites
If you don''t include the <string> header, you cannot use std::string in any context.

“Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.”
— Brian W. Kernighan

##### Share on other sites
Sorry, I wasn''t clear before... I am including "iostream" (with brackets, of course), but not "string". But don''t get any errors until the point where I use cout on the string. I''ll go check again....

Yep, it compiles fine with VC++.NET 2003.

Here is the code:

#include <iostream>int main(){		std::string str("hello");		return 0;}

It seems that everything in the std namespace is there when I include "iostream", but I just can''t cout strings.

##### Share on other sites
You need to include string to use strings.

##### Share on other sites
quote:
Original post by Shard
You need to include string to use strings.

That is what I would think, but you don''t have to include it just to make one.

##### Share on other sites
quote:
Original post by PlayGGY
quote:
Original post by Shard
You need to include string to use strings.

That is what I would think, but you don''t have to include it just to make one.

WHA? Yes you do. Making a string is the same as using it, therefore you have to include <string>

##### Share on other sites
Funny that this should work then

#include <iostream>int main(){   std::string astring="Moo";   std::cout<<astring<<std::endl;   astring+=" I am a cow";   std::cout<<astring<<std::endl;   return 0;}

Compiled with gcc 3.2 on Red Hat Linux

"Give a man a fish and he will eat for a day, drown a man in the water and the fish will eat for a week!

##### Share on other sites
quote:
Original post by cmptrgear
Funny that this should work then

That just shows that GCC''s "iostream" header probably includes
the "string" as well.

That in no way implies that you can depend on this behavior
with other compilers.

Just #include the "string" header to use std::string.

Kami no Itte ga ore ni zettai naru!

• ## Partner Spotlight

• ### Forum Statistics

• Total Topics
627639
• Total Posts
2978344

• 10
• 12
• 22
• 13
• 33