cout << string

This is a discussion on cout << string within the C++ Programming forums, part of the General Programming Boards category; I don't know why this code does not compile: Code: string str = "Hello"; cout << str; It says: no ...

  1. #1
    System Novice siavoshkc's Avatar
    Join Date
    Jan 2006
    Location
    Tehran
    Posts
    1,231

    cout << string

    I don't know why this code does not compile:
    Code:
    string str = "Hello";
    cout << str;
    It says: no operator << found to take right hand operand of type std::string.
    Learn C++ (C++ Books, C Books, FAQ, Forum Search)
    Code painter latest version on sourceforge DOWNLOAD NOW!
    Download FSB Data Integrity Tester.
    Siavosh K C

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,589
    Works for me:
    Code:
    #include <iostream>
    #include <string>
    
    int main()
    {
        using namespace std;
        string str = "Hello";
        cout << str;
    }
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    System Novice siavoshkc's Avatar
    Join Date
    Jan 2006
    Location
    Tehran
    Posts
    1,231
    But not for me.
    Learn C++ (C++ Books, C Books, FAQ, Forum Search)
    Code painter latest version on sourceforge DOWNLOAD NOW!
    Download FSB Data Integrity Tester.
    Siavosh K C

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,589
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    System Novice siavoshkc's Avatar
    Join Date
    Jan 2006
    Location
    Tehran
    Posts
    1,231
    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--------------------------------------------------
    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.
    All things work right except this one. Which compiler option it can be? I had to use str.c_str() to make it work.
    Learn C++ (C++ Books, C Books, FAQ, Forum Search)
    Code painter latest version on sourceforge DOWNLOAD NOW!
    Download FSB Data Integrity Tester.
    Siavosh K C

  6. #6
    System Novice siavoshkc's Avatar
    Join Date
    Jan 2006
    Location
    Tehran
    Posts
    1,231
    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>.
    Learn C++ (C++ Books, C Books, FAQ, Forum Search)
    Code painter latest version on sourceforge DOWNLOAD NOW!
    Download FSB Data Integrity Tester.
    Siavosh K C

  7. #7
    Cat
    Cat is offline
    Registered User
    Join Date
    May 2003
    Posts
    1,571
    Quote Originally Posted by siavoshkc View Post
    But not for me.
    Most likely, you didn't include both <iostream> and <string>, that definition should be in there.
    You ever try a pink golf ball, Wally? Why, the wind shear on a pink ball alone can take the head clean off a 90 pound midget at 300 yards.

  8. #8
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    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++.

  9. #9
    The larch
    Join Date
    May 2006
    Posts
    3,573
    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 might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  10. #10
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,239
    Quote Originally Posted by anon View Post
    Hopefully you didn't add your own implementation to standard headers. This should automatically make your compiler's library implementation non-standard/broken.
    By definition, if the code behaves to spec, it's standard. Period. Doesn't matter who wrote it. But it's not appropriate in this case, since his standard headers are working fine -- he just forgot to include them.

  11. #11
    System Novice siavoshkc's Avatar
    Join Date
    Jan 2006
    Location
    Tehran
    Posts
    1,231
    By definition, if the code behaves to spec, it's standard.
    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!
    Learn C++ (C++ Books, C Books, FAQ, Forum Search)
    Code painter latest version on sourceforge DOWNLOAD NOW!
    Download FSB Data Integrity Tester.
    Siavosh K C

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Inheritance Hierarchy for a Package class
    By twickre in forum C++ Programming
    Replies: 7
    Last Post: 12-08-2007, 03:13 PM
  2. Need help with program.
    By olgirl4life in forum C++ Programming
    Replies: 3
    Last Post: 12-11-2006, 10:06 PM
  3. Compile Error that i dont understand
    By bobthebullet990 in forum C++ Programming
    Replies: 5
    Last Post: 05-05-2006, 09:19 AM
  4. Homework due tonight! help please!!
    By Andy717 in forum C++ Programming
    Replies: 41
    Last Post: 04-07-2005, 01:18 PM
  5. Linked List Help
    By CJ7Mudrover in forum C Programming
    Replies: 9
    Last Post: 03-10-2004, 09:33 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21