CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 11 of 11
  1. #1
    Join Date
    Feb 2004
    Location
    Texas, USA
    Posts
    1,206

    Compiler error for s.clear()

    Consider the following code:

    Code:
    string dup(char c, int n) {
        string s;
        s.clear();
        s.insert(s.begin(), n, c);
        return s; }
    My compiler is: Microsoft Visual C++ 6.0

    The compiler error I receive is:
    h:\c++\proj12ateame\basicutils.h(32) : error C2039: 'clear' : is not a member of 'basic_string<char,struct std::char_traits<char>,class std::allocator<char> >'

    This code works FINE on Microsoft Visual Studio .NET 2003, yet it doesn't on Visual C++ 6.0? If someone could explain to me what's going on, I would be grateful.

    The header files I have included with this code are <string> and <math.h>

    Thanks again guys. You're very helpful.

  2. #2
    Join Date
    Jun 2003
    Location
    Armenia, Yerevan
    Posts
    720
    Code:
    std::string dup(char c, int n)
    {
    	return std::string(n, c);
    }

  3. #3
    Join Date
    Jun 2001
    Location
    Switzerland
    Posts
    4,443
    1. clear() doesn't seem to be a member of std::basic_string, does it?
    2. you don't need something like clear() there, as the string is empty.
    Gabriel, CodeGuru moderator

    Forever trusting who we are
    And nothing else matters
    - Metallica

    Learn about the advantages of std::vector.

  4. #4
    Join Date
    Jun 2003
    Location
    Armenia, Yerevan
    Posts
    720
    However it is strange, I have SGI-STL docs, there is a clear() member in their implementation...

  5. #5
    Join Date
    Jun 2001
    Location
    Switzerland
    Posts
    4,443
    Originally posted by AvDav
    However it is strange, I have SGI-STL docs, there is a clear() member in their implementation...
    I didn't see it in the Dinkumware implementation, nor did I find it in the C++ Standard...
    Gabriel, CodeGuru moderator

    Forever trusting who we are
    And nothing else matters
    - Metallica

    Learn about the advantages of std::vector.

  6. #6
    Join Date
    Aug 2002
    Location
    Madrid
    Posts
    4,588
    Yes, it's not in the standard, but STLPort includes it. I'm wondering why it isn't in the standard though, since it's potentially a useful function.
    Get this small utility to do basic syntax highlighting in vBulletin forums (like Codeguru) easily.
    Supports C++ and VB out of the box, but can be configured for other languages.

  7. #7
    Join Date
    Aug 2000
    Location
    West Virginia
    Posts
    7,721
    Actually, clear() is in the standard ( 21.3.3 , para 12)

    With the "effects: behaves as if the function calls"

    Code:
    erase( begin() , end() );

  8. #8
    Join Date
    Aug 2002
    Location
    Madrid
    Posts
    4,588
    Interesting, that's true. The thing is that it's not listed in the public interface for basic_string nor in the index, which is a bit strange. Maybe this is already addressed in a defect report somewhere.
    Get this small utility to do basic syntax highlighting in vBulletin forums (like Codeguru) easily.
    Supports C++ and VB out of the box, but can be configured for other languages.

  9. #9
    Join Date
    Feb 2004
    Location
    Texas, USA
    Posts
    1,206
    I noticed some people specified Std:: in front of it, but this isn't needed in my case because at the top of the header file that contains this function I used namspace STD.

    I hope someone can help me resolve this issue. I haven't really been able to understand the discussion above, about the SGI-STL and Dinkumware lol

    Thanks again.

  10. #10
    Join Date
    Aug 2002
    Location
    Madrid
    Posts
    4,588
    AvDav gave you the solution already and the rest of the discussion is about the error you get when calling std::string::clear, which doesn't seem to exist in your STL implementation.
    Get this small utility to do basic syntax highlighting in vBulletin forums (like Codeguru) easily.
    Supports C++ and VB out of the box, but can be configured for other languages.

  11. #11
    Join Date
    Jun 2003
    Location
    Gjøvik, Norway
    Posts
    204
    Originally posted by MrDoomMaster (bold added by me)
    I noticed some people specified Std:: in front of it, but this isn't needed in my case because at the top of the header file that contains this function I used namspace STD.
    You shouldn't put "using namespace whatever;" in any header file. Doing that will result in that namespace being used by all files that include your header. Something that in turn can result in name conflicts, since the user of the header doesn't know that the namespace is used.

    If you don't want to write std:: for every function/container/etc you use, put all your code in the cpp file (except the declarations of course), and put using namespace at the top of that instead.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured