I don't know why this code does not compile:It says: no operator << found to take right hand operand of type std::string.Code:string str = "Hello";
cout << str;
Printable View
I don't know why this code does not compile:It says: no operator << found to take right hand operand of type std::string.Code:string str = "Hello";
cout << str;
Works for me:
Code:#include <iostream>
#include <string>
int main()
{
using namespace std;
string str = "Hello";
cout << str;
}
But not for me.
What did you try? What compiler did you use? If Microsoft Visual Studio 2005 Professional gives such an error for my code example, then you must have a serious installation or configuration problem (e.g., you are not running the compiler in standard C++ mode). More likely you made some mistake, e.g., forgot to include a header file.Quote:
But not for me.
I added this to my ostream header and now it works.Code://Sia------------------------------------------------------
template<class _Elem,
class _Traits> inline
basic_ostream<_Elem, _Traits>& __CLRCALL_OR_CDECL operator<<(
basic_ostream<_Elem, _Traits>& _Ostr, const string &cstr)
{ // insert NTBS
const char *_Val = cstr.c_str();
ios_base::iostate _State = ios_base::goodbit;
streamsize _Count = (streamsize)::strlen(_Val); // may overflow
streamsize _Pad = _Ostr.width() <= 0 || _Ostr.width() <= _Count
? 0 : _Ostr.width() - _Count;
const typename basic_ostream<_Elem, _Traits>::sentry _Ok(_Ostr);
if (!_Ok)
_State |= ios_base::badbit;
else
{ // state okay, insert characters
_TRY_IO_BEGIN
const ctype<_Elem>& _Ctype_fac = _USE(_Ostr.getloc(), ctype<_Elem>);
if ((_Ostr.flags() & ios_base::adjustfield) != ios_base::left)
for (; 0 < _Pad; --_Pad) // pad on left
if (_Traits::eq_int_type(_Traits::eof(),
_Ostr.rdbuf()->sputc(_Ostr.fill())))
{ // insertion failed, quit
_State |= ios_base::badbit;
break;
}
for (; _State == ios_base::goodbit && 0 < _Count; --_Count, ++_Val)
if (_Traits::eq_int_type(_Traits::eof(),
_Ostr.rdbuf()->sputc(_Ctype_fac.widen(*_Val))))
_State |= ios_base::badbit;
if (_State == ios_base::goodbit)
for (; 0 < _Pad; --_Pad) // pad on right
if (_Traits::eq_int_type(_Traits::eof(),
_Ostr.rdbuf()->sputc(_Ostr.fill())))
{ // insertion failed, quit
_State |= ios_base::badbit;
break;
}
_Ostr.width(0);
_CATCH_IO_(_Ostr)
}
_Ostr.setstate(_State);
return (_Ostr);
}
//END-Sia--------------------------------------------------
All things work right except this one. Which compiler option it can be? I had to use str.c_str() to make it work.Quote:
What did you try? What compiler did you use? If Microsoft Visual Studio 2005 Professional gives such an error for my code example, then you must have a serious installation or configuration problem (e.g., you are not running the compiler in standard C++ mode). More likely you made some mistake, e.g., forgot to include a header file.
Oops, I found the problem. I'd forgotten to include <string>. Sorry. But I thought I did it because c_str worked. It is because there is a <xstring> header that was included by another header that has the definition of << string. Maybe <iostream>.
On at least one implementation, iostream includes part of the stuff for string, but not the operator<< and >>. This is actually a common problem (your implementation is probably a common one). I'm assuming VC++.
Hopefully you didn't add your own implementation to standard headers. This should automatically make your compiler's library implementation non-standard/broken.
If you get used to your own code (in standard headers) and forget you added it, you might have problems if you need to move to some other implementation. (Because code that you have got used to won't work any longer.)
I found out I was wrong when I made a new project to test laserlight's code. It was after my changes in <ostream>. At compile time, compiler complained for ambiguity. Then I found out there is another definition!Quote:
By definition, if the code behaves to spec, it's standard.