Thread: Error, std::stringstream no member 'c_str'

  1. #1
    Registered User
    Join Date
    May 2016
    Posts
    40

    Error, std::stringstream no member 'c_str'

    Hello I've looked around and there are some posts on this, and I've tried the fixes I found but still getting the error.
    I'm using the Dev_C++ ver 5.11, MinGW compiler, I surmise I may not be able to use c_str with this compiler but figured I'd run it past you guys.

    My entire brief only necessary (but compiled) code is below, showing the error and where it's occurring.

    These are my compiler settings currently, although I've tried them all to no avail.

    CXXFLAGS = $(CXXINCS) -std=gnu++11 -Wall -Wfatal-errors -g3 -fverbose-asm
    CFLAGS = $(INCS) -std=gnu++11 -Wall -Wfatal-errors -g3 -fverbose-asm

    The error is occurring about the last 3rd segment of the code on line 65

    Code:
    #include <iostream>
    #include <iomanip>
    #include <string>
    #include <sstream>
    #include <fstream>
    #include <cstdlib>
    using namespace std;
    
    class Base{
     public:
     int iDbugPrtCt;
     const char *ReVestFileName;
     ofstream DrawDn_File;
     ofstream ReVest_File;
    
     Base();
     ~Base();
    
     void Output();
     int Re_InVestCalc();
    };  // End of Base Class declaration
    
    // Base Ctor
    Base::Base()
    {  iDbugPrtCt = 0;  }
    
    // Base Dtor
    Base::~Base()
     { /*nothing yet*/  }
    
    void Base::Output()
    {
      DrawDn_File.open("DrawDown.txt");
    
      if (!DrawDn_File.good())
      { cerr << "Problem opening DrawDown file"; exit(1);
      }
    
      iDbugPrtCt++ ;
      DrawDn_File << "Entered Output Func, output # " << iDbugPrtCt << "\n";
    
      if (DrawDn_File.fail() )
      { cerr << "Failed write DrawDown file"; exit(1);
      }
    
      Re_InVestCalc();
    
      iDbugPrtCt++ ;
      DrawDn_File << "Back from Re_InVestCalc, output # " << iDbugPrtCt << "\n";
    
      if (DrawDn_File.fail() )
      { cerr << "Failed write DrawDown file"; exit(1); }
    
    } // End of Base::Output Func
    
    int Base::Re_InVestCalc()
    { /* what I want is to insert a numerical counter in the filename 
         since later I will create a new file for each iteration. As you
         see I've having trouble getting it into a const char ptr that 
         ofstream appears to require
      */
      stringstream sItoS;
      sItoS << iDbugPrtCt;   // int to string,
      const char* sFilename2 = "ReInvest_" ;
      sFilename2 += sItoS.c_str + ".txt" ;  //<-Draws error
              // Error, std::stringstream has no member named 'c_str'
    
      ReVestFileName = sFilename;
    
      ReVest_File.open("ReInvest.txt",  ios_base::out | ios_base::app );
    
      if (!ReVest_File.good())
       { cerr << "Problem opening Re-invest file"; exit(1); }
       }
    
      iDbugPrtCt = iDbugPrtCt + 1 ;
      ReVest_File << "Inside Re_InVestCalc, output # " << iDbugPrtCt << "\n";
    
      ReVest_File << "sFilename is " << sFilename  << "\n";
    
      return 0;
     }  //End of Base::Re_InVestCalc Func
    
    int main()
    {
      Base ObjRun;
      ObjRun.Output();
    
      //std::cin.get(); // Only for viewing output in real time.
      return 0;
    }
    Last edited by R_W_B; 06-03-2016 at 04:46 PM. Reason: added error line number

  2. #2
    Guest
    Guest
    The error is quite specific, std::stringstream does not have a c_str member, you're trying to use something that doesn't exist, regardless of your compiler. Also note that c_str – where it exists – is a function (you need to call it), not a variable. There appears to be a str method for the class which should do what you want.

    p.s. you can't do that either:
    Code:
    const char* sFilename2;
    sFilename2 += ...
    Write smaller tests first before introducing things that are new to you into bigger programs.
    Last edited by Guest; 06-03-2016 at 05:21 PM.

  3. #3
    Registered User
    Join Date
    Apr 2011
    Posts
    308
    I fixed some errors and compiled this code in vs 2010 express c++;


    Code:
    #include <iostream>
    #include <iomanip>
    #include <string>
    #include <sstream>
    #include <fstream>
    #include <cstdlib>
    using namespace std;
     
    class Base{
     public:
     int iDbugPrtCt;
     string ReVestFileName;
     ofstream DrawDn_File;
     ofstream ReVest_File;
     
     Base();
     ~Base();
     
     void Output();
     int Re_InVestCalc();
    };  // End of Base Class declaration
     
    // Base Ctor
    Base::Base()
    {  iDbugPrtCt = 0;  }
     
    // Base Dtor
    Base::~Base()
     { /*nothing yet*/  }
     
    void Base::Output()
    {
      DrawDn_File.open("DrawDown.txt");
     
      if (!DrawDn_File.good())
      { cerr << "Problem opening DrawDown file"; exit(1);
      }
     
      iDbugPrtCt++ ;
      DrawDn_File << "Entered Output Func, output # " << iDbugPrtCt << "\n";
     
      if (DrawDn_File.fail() )
      { cerr << "Failed write DrawDown file"; exit(1);
      }
     
      Re_InVestCalc();
     
      iDbugPrtCt++ ;
      DrawDn_File << "Back from Re_InVestCalc, output # " << iDbugPrtCt << "\n";
     
      if (DrawDn_File.fail() )
      { cerr << "Failed write DrawDown file"; exit(1); }
     
    } // End of Base::Output Func
     
    int Base::Re_InVestCalc()
    { /* what I want is to insert a numerical counter in the filename 
         since later I will create a new file for each iteration. As you
         see I've having trouble getting it into a const char ptr that 
         ofstream appears to require
      */
      stringstream sItoS;
      sItoS << iDbugPrtCt;   // int to string,
      string sFilename2 = "ReInvest_" ;
      sItoS << ".txt" ;
      sFilename2 = sItoS.str();  //<-Draws error
              // Error, std::stringstream has no member named 'c_str'
     
      ReVestFileName = sFilename2;
     
      ReVest_File.open("ReInvest.txt",  ios_base::out | ios_base::app );
     
      if (!ReVest_File.good())
       { cerr << "Problem opening Re-invest file"; exit(1); }
       
     
      iDbugPrtCt = iDbugPrtCt + 1 ;
      ReVest_File << "Inside Re_InVestCalc, output # " << iDbugPrtCt << "\n";
     
      ReVest_File << "sFilename is " << sFilename2  << "\n";
     
      return 0;
     }  //End of Base::Re_InVestCalc Func
     
    int main()
    {
      Base ObjRun;
      ObjRun.Output();
     
      //std::cin.get(); // Only for viewing output in real time.
      return 0;
    }

    I hope that helped you.

  4. #4
    Registered User
    Join Date
    May 2016
    Posts
    40
    Ok, sorry, not sure where I foo barred in my google, but it's academic now.

    Thanks to your help though I kept prodding on with how to accomplish building the filename at run time and then get it back to a const char to pass to ofstream. I came up with two options below.

    Now my question is there a better way to do this than either of what I have here ?

    Code:
    stringstream sItoS;
    string sStringName, sStringCt;
    sItoS << iDbugPrtCt;   // int to stringstream
    sItoS >> sStringCt;    // stringstream to string
    sStringName = "Reinvest" + sStringCt + ".txt"; // build string
    ReVestFileName = sStringName.c_str();   // now I use c_str() to get back to const char*
    const string& sAlternate = "Reinvest" + sStringCt + ".txt"; // But apparently could have used this
                                                                // to pass to ofstream.open also
    
    //ReVest_File.open( ReVestFileName,  ios_base::out | ios_base::app ); // this or the below both 
    ReVest_File.open( sAlternate,  ios_base::out | ios_base::app );       // seem to work.

  5. #5
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Not really, although I suppose you could cut down on the number of variables. stringstream lets you build strings, so you may as well take full advantage of that.
    Code:
    stringstream ReVestFileName;
    ReVestFileName << "Reinvest" << iDbugPrtCt << ".txt";
    ReVest_File.open(ReVestFileName.str(), ofstream::app); // ReVestFileName.str().c_str() is also fine
    The file name can be reset at any time by calling ReVestFileName.str("");

    I think the totally object oriented approach is a bit hasty, too, but that is a subject for another time.
    Last edited by whiteflags; 06-04-2016 at 02:02 AM.

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by jeremy duncan
    Now my question is there a better way to do this than either of what I have here ?
    I'd say that whiteflags' suggestion of just using the stringstream to build the string is what stringstreams are for, beyond merely converting from an integer to a string, and it is definitely a better way than what you did.

    That said, especially if you are dealing with more variables to be formatted or with more complex formatting, you could consider Boost.Format, which is implemented using stringstreams. In your case, this could simplify the code to:
    Code:
    ReVest_File.open(str(boost::format("Reinvest_%1%.txt") % iDbugPrtCt), ofstream::app);
    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

  7. #7
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    I'll also mention there's std::to_string to convert integers to strings. And VS 2010 is quite old, so strongly consider upgrading.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  8. #8
    Registered User
    Join Date
    May 2016
    Posts
    40
    Quote Originally Posted by whiteflags View Post
    Not really, although I suppose you could cut down on the number of variables. stringstream lets you build strings, so you may as well take full advantage of that.
    Code:
    stringstream ReVestFileName;
    ReVestFileName << "Reinvest" << iDbugPrtCt << ".txt";
    ReVest_File.open(ReVestFileName.str(), ofstream::app); // ReVestFileName.str().c_str() is also fine
    The file name can be reset at any time by calling ReVestFileName.str("");

    I think the totally object oriented approach is a bit hasty, too, but that is a subject for another time.
    Well yea there really was no reason to write a full blown class for this, just wanted to. I had programmed to a hobby novice level about 6 yrs ago in C++ paradigm, but health issues put me down for a long time. I'm retired and trying to pick it up again just to keep busy. And I like writing stuff for my curiousities. I used to work for a civil engineering company so I have lots of routines I could make use of coding to share with those still working. You guys are kind to give me your time, I'll try not to waste it.

    Quote Originally Posted by laserlight View Post
    I'd say that whiteflags' suggestion of just using the stringstream to build the string is what stringstreams are for, beyond merely converting from an integer to a string, and it is definitely a better way than what you did.

    That said, especially if you are dealing with more variables to be formatted or with more complex formatting, you could consider Boost.Format, which is implemented using stringstreams. In your case, this could simplify the code to:
    Code:
    ReVest_File.open(str(boost::format("Reinvest_%1%.txt") % iDbugPrtCt), ofstream::app);
    Thanks to both you guys, cool, so while I'm banging my way back to c_str(), stringstream.str() returns the same compatible type to ofstream.open all along.

    Quote Originally Posted by Elysia View Post
    I'll also mention there's std::to_string to convert integers to strings. And VS 2010 is quite old, so strongly consider upgrading.
    Thanks I knew about it's type conversion abilities. And the VS2010 (lol) well I'm just a novice on free Dev C, it's ok most of the time, some times I have to reset the attributes on the output file for some reason to get it to compile & Link. I do have a Pro copy of VS 2005 on a CD, but haven't installed it yet.

    Hey what font do you guys use in your IDE's ? I like Consolas, it's a fixed width but I hate that it's lower case l is the same as tne number 1.

  9. #9
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Thanks to both you guys, cool, so while I'm banging my way back to c_str(), stringstream.str() returns the same compatible type to ofstream.open all along.
    fstream::open() is an init method basically. stringstream::str() did not return an instance of ofstream, as you make it sound. Rather, it returned a string, representing the (implicit) path and file name you wanted to open. You see this when you look at open's signature:
    ofstream::open - C++ Reference
    Code:
    void open (const   char* filename,  ios_base::openmode mode = ios_base::out);
    void open (const string& filename,  ios_base::openmode mode = ios_base::out);
    In normal code you would call one of these.

  10. #10
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by R_W_B View Post
    And the VS2010 (lol) well I'm just a novice on free Dev C, it's ok most of the time, some times I have to reset the attributes on the output file for some reason to get it to compile & Link. I do have a Pro copy of VS 2005 on a CD, but haven't installed it yet.
    Get VS2015 Community. It's free.

    Hey what font do you guys use in your IDE's ? I like Consolas, it's a fixed width but I hate that it's lower case l is the same as tne number 1.
    Default font.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  11. #11
    Registered User
    Join Date
    May 2016
    Posts
    40
    Quote Originally Posted by whiteflags View Post
    fstream:pen() is an init method basically. stringstream::str() did not return an instance of ofstream, as you make it sound. Rather, it returned a string, representing the (implicit) path and file name you wanted to open. You see this when you look at open's signature:
    ofstream:pen - C++ Reference
    Code:
    void open (const   char* filename,  ios_base::openmode mode = ios_base::out);
    void open (const string& filename,  ios_base::openmode mode = ios_base::out);
    In normal code you would call one of these.
    Thanks again, actually I meant what you said in that it returns "filename" of the required arg type. I didn't mean instance of the object. I guess I just don't speak it right.

    Quote Originally Posted by Elysia View Post
    Get VS2015 Community. It's free.

    Default font.
    Thanks but won't the free version require anyone I share my routines with have "whatever" installed for managed code ? I.e. it doesn't produce stand alone native code does it ?

    Obviously I'm not writing anything real big, just small stuff to make the jobs I used to do easier for those who still do it.

  12. #12
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    C++ is still entirely native in VS2015. Have no fear. There's no managed code.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  13. #13
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by R_W_B
    Hey what font do you guys use in your IDE's ? I like Consolas, it's a fixed width but I hate that it's lower case l is the same as tne number 1.
    I chose DejaVu Sans Mono precisely because it has good visual differentiation between lower case l and the numeral 1.
    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

  14. #14
    Registered User
    Join Date
    May 2016
    Posts
    40
    Quote Originally Posted by Elysia View Post
    C++ is still entirely native in VS2015. Have no fear. There's no managed code.
    Oh cool, didn't think it was that way. Guess it's just maybe less bells like no class wizard or no MFC or whatever. Course don't need any of that right now. Currently I'm just short standard C++ console apps that either output to screen or file.

    I will check VS2015 free out down the road a bit.

    Quote Originally Posted by laserlight View Post
    I chose DejaVu Sans Mono precisely because it has good visual differentiation between lower case l and the numeral 1.
    Thanks I will go try that one right now. So far every mono space I've tried with the small case of L different than 1, had too wide spaced.

    ( Oh BTW just to make sure, I don't see any -Thanks- or -Rep_ buttons on here. Am I blind or does this forum not have all that thank click stuff ? )
    Anyhows I do appreciate you guys time.
    Last edited by R_W_B; 06-05-2016 at 09:22 AM. Reason: Added question on thanking logistics

  15. #15
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Those buttons disappeared a while ago due to an upgrade.
    See this thread for more info.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. What does c_str() do?
    By Eman in forum C++ Programming
    Replies: 16
    Last Post: 01-18-2011, 09:17 AM
  2. c_str()
    By darren78 in forum C++ Programming
    Replies: 2
    Last Post: 08-14-2010, 03:07 AM
  3. c_str problem
    By Dynamitemedina in forum C++ Programming
    Replies: 1
    Last Post: 02-06-2010, 07:36 PM
  4. c_str() problem
    By h3ro in forum C++ Programming
    Replies: 12
    Last Post: 05-15-2007, 05:26 AM
  5. *this and .c_str()
    By stalker in forum C++ Programming
    Replies: 3
    Last Post: 01-19-2004, 03:13 PM

Tags for this Thread