Thread: Book says that "theString.clear();" is in <string> Compiler says no. What can I do?

  1. #1
    Registered User
    Join Date
    Oct 2001

    Unhappy Book says that "theString.clear();" is in <string> Compiler says no. What can I do?


    My first time here.

    I have a very old degee in CPS and am taking a C++ class and I want to clear my string. I don't want to
    thestring = ' ';
    because I want to concatenate other strings on to this. I want the string to be totally empty.

    My textbook says that there are a ton of member functions to work with strings. one of them is:
    The book says it is in <string>. But when I go to compile I get this error message:
    E:\SUSAN\C++\Lab4.cpp(57) : error C2039: 'clear' : is not a member of 'basic_string<char,struct std::char_traits<char>,class std::allocator<char> >'

    Can someone here please tell me what this means?
    I looked at all the .h files on my computer and I don't see any other ones to do string manipulation. I read the .h file and I did not see any mention of the ".clear()" function.

    I am most grateful.


  2. #2
    geek SilentStrike's Avatar
    Join Date
    Aug 2001

    std::string whatever = "";

    Such will be essentially blank... there is no leading space. It will be fine for concatation.

    whatever+= "Hello ";
    whatever+= "World";

    will output cause the string to contain "Hello World", as expected.
    Prove you can code in C++ or C# at TopCoder, referrer rrenaud
    Read my livejournal

  3. #3
    Registered User
    Join Date
    Oct 2001
    Use erase() instead of clear() .

    I don't know where your book took it, but in STL, the efficient way to clear a string is theString.erase();

  4. #4
    Registered User
    Join Date
    Oct 2001
    Dear Shmulik,

    Ya, hoo.
    theString.erase(); worked perfect. I think that using.erase() is more explicit than just setting the string = to a null quote.

    Thank you. I also wonder where clear() came from in my textbook?

    You know how the little window pops up when you type a
    dot after a variable, well clear was not one of my options but erase was on the list.

    I see you are new here, as I am, and I really appreciate the time you took to respond.


Popular pages Recent additions subscribe to a feed

Similar Threads

  1. URGENT: Help C
    By iamjimjohn in forum C Programming
    Replies: 16
    Last Post: 05-18-2007, 05:46 AM
  2. C Compiler and stuff
    By pal1ndr0me in forum C Programming
    Replies: 10
    Last Post: 07-21-2006, 11:07 AM
  3. Please Help!!!!!
    By princssashes in forum C# Programming
    Replies: 2
    Last Post: 04-05-2006, 08:18 AM
  4. I"m selling a book...
    By St0rmTroop3er in forum Game Programming
    Replies: 2
    Last Post: 12-13-2003, 01:55 PM
  5. Compiler Design... Anyone That Can Help!
    By ComputerNerd888 in forum C++ Programming
    Replies: 3
    Last Post: 09-27-2003, 09:48 AM