CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8
  1. #1
    Join Date
    Feb 2009
    Posts
    135

    Using string.erase() incorrectly?

    Here's the code:
    Code:
                         if (x + phrase.size() >= max_width || y >= max_height)
                         {
                           string::size_type phrase_size = phrase.size();
                           string::size_type over_flow = (max_width + x) - phrase_size;
                           phrase.erase(over_flow);
                           x = max_width;
                         }
                         else
                         {
                          cout << phrase;
                          x += phrase.size();
                         }
    The error is:
    Line: 42
    error: passing `const std::string' as `this' argument of `std::basic_string<_CharT, _Traits, _Alloc>& std::basic_string<_CharT, _Traits, _Alloc>::erase(typename _Alloc::size_type, typename _Alloc::size_type) [with _CharT = char, _Traits = std::char_traits<char>, _Alloc = std::allocator<char>]' discards qualifiers|
    ||=== Build finished: 1 errors, 0 warnings ===|

    I get from this that I'm not passing an argument correctly, or something, but I'm unsure of why. How can I fix it? It's supposed to take:

    Code:
    -----------------------
    |                          |
    |                          |
    |                  Some Text
    -----------------------
    
    into
    
    -----------------------
    |                          |
    |                          |
    |                  Some
    -----------------------
    
    eventually I want it to be
    
    -----------------------
    |                          |
    |                          |
    |                   Som|
    -----------------------

  2. #2
    Join Date
    Feb 2002
    Posts
    4,640

    Re: Using string.erase() incorrectly?

    What is "phrase" declared as?

    Viggy

  3. #3
    Join Date
    Aug 2005
    Location
    San Diego, CA
    Posts
    1,054

    Re: Using string.erase() incorrectly?

    Post a more complete example that contains the definitions of the variables in the snip and tell us what compiler you are using. I don't see any problem with the erase call. You can certainly use that call with a single argument that will chop off the end of the string starting with the position that you specify.

    As for what that particular compiler error is related to, I have no idea. It seems like this is a recurring problem here. People will post a small snip of code and a compiler error or problem description and then we have to guess as to what the rest of the program is doing. That won't work unless there is something totally and obviously wrong with the small code snip that you post.

  4. #4
    Join Date
    Feb 2002
    Posts
    4,640

    Re: Using string.erase() incorrectly?

    Well, the error may have a clue:
    error: passing `const std::string' as `this' argument


    Viggy

  5. #5
    Join Date
    Feb 2009
    Posts
    135

    Re: Using string.erase() incorrectly?

    Sorry, I felt that posting whole code was spammy. You were right, I was using const. I fixed that, ran it, and it terminated unusually at that line...

    Code:
    #include <iostream>
    #include <string>
    
    using std::cout;    using std::cin;
    using std::string;  using std::endl;
    
    int main()
    {
        const int max_width = 40, max_height = 10;
        string phrase = "This is a test phrase";
    
        /* Invariant, y is equal to the rows, once the number
           of rows reaches the max, the loop will break */
        for (int y = 0; y != max_height; ++y)
         {
              int x = 0;
             /* Invariant, x is equal to the columns, once the
                columns have reached the max_width, the loop
                will break, but a new row will form */
             while (x != max_width)
              {
                if (y == 0 || y == max_height - 1)
                 {
                   cout << "-";
                   ++x;
                 }
                else
                 {
                   if (x == 0 || x == max_width - 1)
                    {
                      cout << "|";
                      ++x;
                    }
                   else
                    {
                      if (x == 21 && y == 8)
                       {
                         if (x + phrase.size() >= max_width || y >= max_height)
                         {
                           string::size_type phrase_size = phrase.size();
                           string::size_type over_flow = (max_width + x) - phrase_size;
                           phrase.erase(over_flow);
                           x = max_width;
                         }
                         else
                         {
                          cout << phrase;
                          x += phrase.size();
                         }
                       }
                      else
                       {
                         cout << " ";
                         ++x;
                       }
                    }
                 }
              }
           cout << endl;
         }
    
        cout << endl;
        system("PAUSE");
        return 0;
    }

  6. #6
    Join Date
    Nov 2008
    Location
    England
    Posts
    748

    Re: Using string.erase() incorrectly?

    the erase tries to erase the 40th character in a string of size 21. You are writing in memory thats not owned by you, hence the exception. Learn to use the debugger.
    Get Microsoft Visual C++ Express here or CodeBlocks here.
    Get STLFilt here to radically improve error messages when using the STL.
    Get these two can't live without C++ libraries, BOOST here and Loki here.
    Check your code with the Comeau Compiler and FlexeLint for standards compliance and some subtle errors.
    Always use [code] code tags [/code] to make code legible and preserve indentation.
    Do not ask for help writing destructive software such as viruses, gamehacks, keyloggers and the suchlike.

  7. #7
    Join Date
    Feb 2009
    Posts
    135

    Re: Using string.erase() incorrectly?

    Quote Originally Posted by Russco View Post
    the erase tries to erase the 40th character in a string of size 21. You are writing in memory thats not owned by you, hence the exception. Learn to use the debugger.
    I'm going to! ...just haven't had time.

  8. #8
    Join Date
    Nov 2008
    Location
    England
    Posts
    748

    Re: Using string.erase() incorrectly?

    I think this is what you were trying to do...
    Code:
    #include <iostream>
    #include <string>
    
    using std::cout;    using std::cin;
    using std::string;  using std::endl;
    
    int main()
    {
        const int max_column = 40, max_row = 10;
        string phrase = "This is a test phrase";
    
        for (int row = 0; row < max_row; ++row)
        {
            int column = 0;
            
            while (column < max_column)
            {
                if (row == 0 || row == max_row - 1 )
                {
                    cout << "-";
                }
                else
                {
                    if (column == 0 || column == max_column - 1 )
                    {
                        cout << "|";
                    }
                    else
                    {
                        if ( row == 8 && column == 21 )
                        {
                            while ( column + phrase.size() > max_column ) 
                            {
                                string::size_type over_flow =  max_column - column - 1;
                                phrase.erase(over_flow);
                            }
                            cout << phrase;
                            column = max_column - 2;
                        }
                        else
                        {
                            cout << " ";
                        }
                    }
                }
    
                ++column; 
            }
            cout << endl;
        }
    
        cout << endl;
        system("PAUSE");
        return 0;
    }
    Get Microsoft Visual C++ Express here or CodeBlocks here.
    Get STLFilt here to radically improve error messages when using the STL.
    Get these two can't live without C++ libraries, BOOST here and Loki here.
    Check your code with the Comeau Compiler and FlexeLint for standards compliance and some subtle errors.
    Always use [code] code tags [/code] to make code legible and preserve indentation.
    Do not ask for help writing destructive software such as viruses, gamehacks, keyloggers and the suchlike.

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