Thread: Question about std::string..

  1. #1
    Absent Minded Programmer
    Join Date
    May 2005
    Posts
    968

    Question about std::string..

    Hi I'm curious as to how you would display an std::string using cout, this bit of code gives me an error "<< no operator found which takes a righthand operand type "std::string""

    Code:
    class TextEngine
    {
    	void Respond()
    	{
    		std::cout << Response << std::endl;
    	}
    	std::string Response;
    };
    I'm a noob to std:: string so maybe I'm doing this wrong...
    Sometimes I forget what I am doing when I enter a room, actually, quite often.

  2. #2
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    Code:
    #include <iostream>
    
    int main()
    {
    	std::string str("Hello, world!");
    	
    	std::cout << str << std::endl;
    	
    	return 0;
    }
    If this compiles and runs, you have a different problem. Make sure you're including iostream and possibly the string header string.

  3. #3
    Absent Minded Programmer
    Join Date
    May 2005
    Posts
    968
    Code:
    #include <string.h>
    #include <iostream>
    
    class TextEngine
    {
    	void Respond()
    	{
    		std::cout << Response << std::endl;
    	}
    	std::string Response;
    
    };
    Thats the whole thing, and it doesn't work.... I wonder...
    Sometimes I forget what I am doing when I enter a room, actually, quite often.

  4. #4
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    Don't include string.h. The .h extension is from older days. Just do this:

    Code:
    #include <string>

  5. #5
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    All of the class members are private, so calling Respond() outside of other class member functions is impossible. Might I add that MacGuyver has the simplest idea with the least boilerplate code.

  6. #6
    Absent Minded Programmer
    Join Date
    May 2005
    Posts
    968
    I forgot about string and string.h

    Ooops! :d
    Sometimes I forget what I am doing when I enter a room, actually, quite often.

  7. #7
    Registered User
    Join Date
    May 2007
    Posts
    13
    Quote Originally Posted by Shamino View Post
    I forgot about string and string.h

    Ooops! :d
    I guess that's why your the Absent minded programmer

  8. #8
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    Some compilers allow you to use string by only including <iostream>. Others let you use parts of string but not the overload operators (which probably happened here). The safest and most correct thing of course is to just always #include <string> when you use the string class.

    >> Don't include string.h. The .h extension is from older days.
    To be technical, <string.h> is from C, and is standard but deprecated in C++. The preferred version of <string.h> in C++ is <cstring>. However, these two headers are unrelated to <string>. Most programs will use one or the other, but it is possible you would need both since they contain different things.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Alice....
    By Lurker in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 06-20-2005, 02:51 PM
  2. Debugging help
    By cuddlez.ini in forum C++ Programming
    Replies: 3
    Last Post: 10-24-2004, 07:08 PM
  3. DLL and std::string woes!
    By Magos in forum C++ Programming
    Replies: 7
    Last Post: 09-08-2004, 12:34 PM
  4. Question...
    By TechWins in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 07-28-2003, 09:47 PM
  5. returning std::string
    By Unregistered in forum C++ Programming
    Replies: 3
    Last Post: 09-24-2001, 08:31 PM