Thread: cout << string

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

    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
    28,413
    Works for me:
    Code:
    #include <iostream>
    #include <string>
    
    int main()
    {
        using namespace std;
        string str = "Hello";
        cout << str;
    }
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    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,246
    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
    28,413
    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 Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    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,246
    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,246
    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
    Registered User
    Join Date
    May 2003
    Posts
    1,619
    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,366
    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
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    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,246
    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, 04:13 PM
  2. Need help with program.
    By olgirl4life in forum C++ Programming
    Replies: 3
    Last Post: 12-11-2006, 11: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, 10:33 PM