Thread: how do i convert stringstream to string?

  1. #1
    Registered User
    Join Date
    Aug 2009
    Posts
    30

    Unhappy how do i convert stringstream to string?

    How do I convert from std::stringstream to std::string in C++?

    Do I need to call a method on the string stream?

    i tried .str() but didnt work.

    Code:
    //cpp
    
    Document*Repository::addDoc(iostream& inStream,string docId)
    
    //test core 
    
    Repository r1; 
    stringstream ss; 
      
    string s1 = "Hello World;" 
      
    ss.clear(); 
    ss<<s1; 
    r1.addDoc(ss,"1")
    it copies string into stream and pass it as argument for the method addDoc above.when i tried to convert it back to string it didnt work saying .str() is not a member of &inStream.

    but why is that since it is already been convert to stream in the test core and should be passed as stream form for the method.

    can anyone explain this to me?

    thanks

  2. #2
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    What were you trying?
    This should work:
    Code:
    s1 = ss.str();
    "I am probably the laziest programmer on the planet, a fact with which anyone who has ever seen my code will agree." - esbo, 11/15/2008

    "the internet is a scary place to be thats why i dont use it much." - billet, 03/17/2010

  3. #3
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Why do you have "iostream" in your function if you want it to be a stringstream?

  4. #4
    Registered User
    Join Date
    Aug 2009
    Posts
    30
    Quote Originally Posted by cpjust View Post
    What were you trying?
    This should work:
    Code:
    s1 = ss.str();
    i tried but it says &inStream doesnt have .str() member

  5. #5
    Registered User
    Join Date
    Aug 2009
    Posts
    30
    Quote Originally Posted by tabstop View Post
    Why do you have "iostream" in your function if you want it to be a stringstream?
    thats the function given to me.as far as i know iostream& is in stringstream as ss is in that form from the given test core.i could be wrong.

  6. #6
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    The compiler doesn't know that you've passed it a stringstream (and iostream doesn't have a 'str' member function). Either change the function signature or else cast the parameter to a reference to stringstream (prefer the former; much more type-safe).

  7. #7
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    If you want to use the str() method, you need a stringstream&, not an iostream&.

    You can alternatively use the iostream like you would cin to read in strings, using getline() or operator>>.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  8. #8
    Registered User
    Join Date
    Aug 2009
    Posts
    30
    i'll try getline().thank you everyone.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Convert string of characters to integers
    By Digital_20 in forum C++ Programming
    Replies: 2
    Last Post: 04-02-2008, 09:40 PM
  2. Replies: 4
    Last Post: 03-03-2006, 02:11 AM
  3. Classes inheretance problem...
    By NANO in forum C++ Programming
    Replies: 12
    Last Post: 12-09-2002, 03:23 PM
  4. ........ed off at functions
    By Klinerr1 in forum C++ Programming
    Replies: 8
    Last Post: 07-29-2002, 09:37 PM
  5. Warnings, warnings, warnings?
    By spentdome in forum C Programming
    Replies: 25
    Last Post: 05-27-2002, 06:49 PM