'Cout'ing a string requires you to include "string"?
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]
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:
It seems that everything in the std namespace is there when I include "iostream", but I just can''t cout strings.
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.
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.
quote:Original post by PlayGGYquote: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
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!
#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!
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
Popular Topics
Advertisement