Click to See Complete Forum and Search --> : std::string IO


ajbharani
July 7th, 2008, 04:07 AM
#include<iostream>
int main()
{
std::string szName;
std::cin >> szName;
std::cout << szName << std::endl;
return 0;
}

This code, while compilation gives the following error.

--------------------Configuration: BasicString - Win32 Debug--------------------
Compiling...
Test.cpp
d:\bharani\programming\c++\basicstring\test.cpp(6) : error C2679: binary '>>' : no operator defined which takes a right-hand operand of type 'class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >' (or there is no ac
ceptable conversion)
d:\bharani\programming\c++\basicstring\test.cpp(7) : error C2679: binary '<<' : no operator defined which takes a right-hand operand of type 'class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >' (or there is no ac
ceptable conversion)
Error executing cl.exe.
Test.obj - 2 error(s), 0 warning(s)

What may be the problem?

Thanks.

Philip Nicoletti
July 7th, 2008, 04:59 AM
Yiu need to:


#include <string>

ajbharani
July 7th, 2008, 05:17 AM
Thanks. So is the intellisence independant of the headers we have included?

Paul McKenzie
July 7th, 2008, 09:57 AM
Thanks. So is the intellisence independant of the headers we have included?Intellisense is not C++.

If you're going to use std::string, then you must include the <string> header. Don't solely rely on third-party tools such as Intellisense to determine proper C++ coding.

Regards,

Paul McKenzie

Zaccheus
July 7th, 2008, 10:49 AM
To be fair that's a rather silly error message. "std::string not defined" would have been more intuitive.

Paul McKenzie
July 7th, 2008, 11:00 AM
To be fair that's a rather silly error message. "std::string not defined" would have been more intuitive.I think that std::string is forward declared within the stream headers, so it is difficult for the compiler to emit such an error.

Regards,

Paul McKenzie

Zaccheus
July 7th, 2008, 12:55 PM
Ah yes, and such an operator could have been declared using just such a forward declaration, good point.