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

Started by
6 comments, last by PlayGGY 20 years, 1 month ago
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]
And the rockets' red glare, the bombs bursting in air,gave proof through the fight that our flag was still there.Oh say, does that star-spangled banner yet waveover the land of the free and the home of the brave?
Advertisement
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
"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
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.
And the rockets' red glare, the bombs bursting in air,gave proof through the fight that our flag was still there.Oh say, does that star-spangled banner yet waveover the land of the free and the home of the brave?
You need to include string to use strings.
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.
And the rockets' red glare, the bombs bursting in air,gave proof through the fight that our flag was still there.Oh say, does that star-spangled banner yet waveover the land of the free and the home of the brave?
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>
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!
"Pfft, Facts! Facts can be used to prove anything!" -- Homer J. Simpson
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!
神はサイコロを振らない!

This topic is closed to new replies.

Advertisement