-
February 18th, 2009, 03:40 PM
#1
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|
-----------------------
-
February 18th, 2009, 04:03 PM
#2
Re: Using string.erase() incorrectly?
What is "phrase" declared as?
Viggy
-
February 18th, 2009, 04:11 PM
#3
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.
-
February 18th, 2009, 04:16 PM
#4
Re: Using string.erase() incorrectly?
Well, the error may have a clue:
error: passing ` const std::string' as `this' argument
Viggy
-
February 18th, 2009, 04:52 PM
#5
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;
}
-
February 18th, 2009, 05:19 PM
#6
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.
-
February 18th, 2009, 05:22 PM
#7
Re: Using string.erase() incorrectly?
Originally Posted by Russco
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.
-
February 18th, 2009, 09:46 PM
#8
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|