Erasin Vector Element

J

Jonathan

Hey again everyone! I have another question for you guys. I am trying to
erase a certain vector element based on what number the user selects.
Here is what I did:

case 'b' :
{
cout << "Please enter note number to clear: ";
cin >> clear_note;
clear_note = clear_note - 1;
vec.erase(clear_note);
ofstream rewrite("post.txt", ios::eek:ut);
while (! rewrite) //check file open
{
cout << "Error opening output file!";
return -1;
}
while (j < vec.size()) // j is counter variable (set to 0 above)
{
rewrite << vec[j] << endl;
j++;
}
rewrite.close();
break;
}

When I do that, I get the following compile error:

Compiler: Default compiler
Executing g++.exe...
g++.exe "C:\C++\notes.cpp" -o "C:\C++\notes.exe" -g3
-I"C:\Dev-Cpp\include\c++" -I"C:\Dev-Cpp\include\c++\mingw32"
-I"C:\Dev-Cpp\include\c++\backward" -I"C:\Dev-Cpp\include"
-L"C:\Dev-Cpp\lib"
C:/C++/notes.cpp: In function `int clear()':
C:/C++/notes.cpp:142: no matching function for call to `
std::vector<std::string, std::allocator<std::string> >::erase(int&)'
C:/Dev-Cpp/include/c++/bits/stl_vector.h:645: candidates are:
__gnu_cxx::__normal_iterator<_Tp*, std::vector<_Tp, _Alloc> >
std::vector<_Tp, _Alloc>::erase(__gnu_cxx::__normal_iterator<_Tp*,
std::vector<_Tp, _Alloc> >) [with _Tp = std::string, _Alloc =
std::allocator<std::string>]

C:/Dev-Cpp/include/c++/bits/stl_vector.h:668:

__gnu_cxx::__normal_iterator<_Tp*, std::vector<_Tp, _Alloc> >
std::vector<_Tp, _Alloc>::erase(__gnu_cxx::__normal_iterator<_Tp*,
std::vector<_Tp, _Alloc> >, __gnu_cxx::__normal_iterator<_Tp*,
std::vector<_Tp, _Alloc> >) [with _Tp = std::string, _Alloc =
std::allocator<std::string>]

Execution terminated

Any ideas as to why it is doing that? Thanks in advance for any help!!

-Jonathan
 
V

Victor Bazarov

Jonathan said:
Hey again everyone! I have another question for you guys. I am trying to
erase a certain vector element based on what number the user selects.
Here is what I did:

case 'b' :
{
cout << "Please enter note number to clear: ";
cin >> clear_note;
clear_note = clear_note - 1;
vec.erase(clear_note);
ofstream rewrite("post.txt", ios::eek:ut);
while (! rewrite) //check file open
{
cout << "Error opening output file!";
return -1;
}
while (j < vec.size()) // j is counter variable (set to 0 above)
{
rewrite << vec[j] << endl;
j++;
}
rewrite.close();
break;
}

When I do that, I get the following compile error:

Compiler: Default compiler
Executing g++.exe...
g++.exe "C:\C++\notes.cpp" -o "C:\C++\notes.exe" -g3
-I"C:\Dev-Cpp\include\c++" -I"C:\Dev-Cpp\include\c++\mingw32"
-I"C:\Dev-Cpp\include\c++\backward" -I"C:\Dev-Cpp\include"
-L"C:\Dev-Cpp\lib"
C:/C++/notes.cpp: In function `int clear()':
C:/C++/notes.cpp:142: no matching function for call to `
std::vector<std::string, std::allocator<std::string> >::erase(int&)'
C:/Dev-Cpp/include/c++/bits/stl_vector.h:645: candidates are:
__gnu_cxx::__normal_iterator<_Tp*, std::vector<_Tp, _Alloc> >
std::vector<_Tp, _Alloc>::erase(__gnu_cxx::__normal_iterator<_Tp*,
std::vector<_Tp, _Alloc> >) [with _Tp = std::string, _Alloc =
std::allocator<std::string>]

C:/Dev-Cpp/include/c++/bits/stl_vector.h:668:

__gnu_cxx::__normal_iterator<_Tp*, std::vector<_Tp, _Alloc> >
std::vector<_Tp, _Alloc>::erase(__gnu_cxx::__normal_iterator<_Tp*,
std::vector<_Tp, _Alloc> >, __gnu_cxx::__normal_iterator<_Tp*,
std::vector<_Tp, _Alloc> >) [with _Tp = std::string, _Alloc =
std::allocator<std::string>]

Execution terminated

Any ideas as to why it is doing that? Thanks in advance for any help!!

RTFM, will you? There is no 'erase' member in 'std::vector' that takes
a reference to int, or a reference to const int, or an int. The only
argument you're allowed to pass is an _iterator_.

You can say

vec.erase(vec.begin() + clear_note);

to do what you need. But please, RTFM next time before posting.

Victor
 
R

Rolf Magnus

Jonathan said:
Hey again everyone! I have another question for you guys. I am trying
to erase a certain vector element based on what number the user
selects.

And how is that element based on the number? It that number the
element's value? Or its index in the vector?
Here is what I did:

case 'b' :
{
cout << "Please enter note number to clear: ";
cin >> clear_note;
clear_note = clear_note - 1;
vec.erase(clear_note);
ofstream rewrite("post.txt", ios::eek:ut);
while (! rewrite) //check file open
{
cout << "Error opening output file!";
return -1;
}
while (j < vec.size()) // j is counter variable (set to 0
above)

Any reason why you're not using a for loop here?

for (int j = 0; j < vec.size(); ++j)
{
rewrite << vec[j] << endl;
j++;
}
rewrite.close();
break;
}

When I do that, I get the following compile error:

Compiler: Default compiler
Executing g++.exe...
g++.exe "C:\C++\notes.cpp" -o "C:\C++\notes.exe" -g3
-I"C:\Dev-Cpp\include\c++" -I"C:\Dev-Cpp\include\c++\mingw32"
-I"C:\Dev-Cpp\include\c++\backward" -I"C:\Dev-Cpp\include"
-L"C:\Dev-Cpp\lib"
C:/C++/notes.cpp: In function `int clear()':
C:/C++/notes.cpp:142: no matching function for call to `
std::vector said:
::erase(int&)'

The compiler tells you what the problem is. There is no member function
erase in std::vector that takes an int.
C:/Dev-Cpp/include/c++/bits/stl_vector.h:645: candidates are:
__gnu_cxx::__normal_iterator<_Tp*, std::vector<_Tp, _Alloc> >
std::vector<_Tp, _Alloc>::erase(__gnu_cxx::__normal_iterator<_Tp*,
std::vector<_Tp, _Alloc> >) [with _Tp = std::string, _Alloc =
std::allocator<std::string>]

Thre is only one that takes an iterator...
C:/Dev-Cpp/include/c++/bits/stl_vector.h:668:

__gnu_cxx::__normal_iterator<_Tp*, std::vector<_Tp, _Alloc> >
std::vector<_Tp, _Alloc>::erase(__gnu_cxx::__normal_iterator<_Tp*,
std::vector<_Tp, _Alloc> >, __gnu_cxx::__normal_iterator<_Tp*,
std::vector<_Tp, _Alloc> >) [with _Tp = std::string, _Alloc =
std::allocator<std::string>]

and one that takes two iterators.
 
J

Jonathan

Rolf said:
Jonathan wrote:

Hey again everyone! I have another question for you guys. I am trying
to erase a certain vector element based on what number the user
selects.


And how is that element based on the number? It that number the
element's value? Or its index in the vector?

Here is what I did:

case 'b' :
{
cout << "Please enter note number to clear: ";
cin >> clear_note;
clear_note = clear_note - 1;
vec.erase(clear_note);
ofstream rewrite("post.txt", ios::eek:ut);
while (! rewrite) //check file open
{
cout << "Error opening output file!";
return -1;
}
while (j < vec.size()) // j is counter variable (set to 0
above)


Any reason why you're not using a for loop here?

for (int j = 0; j < vec.size(); ++j)

{
rewrite << vec[j] << endl;
j++;
}
rewrite.close();
break;
}

When I do that, I get the following compile error:

Compiler: Default compiler
Executing g++.exe...
g++.exe "C:\C++\notes.cpp" -o "C:\C++\notes.exe" -g3
-I"C:\Dev-Cpp\include\c++" -I"C:\Dev-Cpp\include\c++\mingw32"
-I"C:\Dev-Cpp\include\c++\backward" -I"C:\Dev-Cpp\include"
-L"C:\Dev-Cpp\lib"
C:/C++/notes.cpp: In function `int clear()':
C:/C++/notes.cpp:142: no matching function for call to `
std::vector said:
::erase(int&)'


The compiler tells you what the problem is. There is no member function
erase in std::vector that takes an int.

C:/Dev-Cpp/include/c++/bits/stl_vector.h:645: candidates are:
__gnu_cxx::__normal_iterator<_Tp*, std::vector<_Tp, _Alloc> >
std::vector<_Tp, _Alloc>::erase(__gnu_cxx::__normal_iterator<_Tp*,
std::vector<_Tp, _Alloc> >) [with _Tp = std::string, _Alloc =
std::allocator<std::string>]


Thre is only one that takes an iterator...

C:/Dev-Cpp/include/c++/bits/stl_vector.h:668:

__gnu_cxx::__normal_iterator<_Tp*, std::vector<_Tp, _Alloc> >
std::vector<_Tp, _Alloc>::erase(__gnu_cxx::__normal_iterator<_Tp*,
std::vector<_Tp, _Alloc> >, __gnu_cxx::__normal_iterator<_Tp*,
std::vector<_Tp, _Alloc> >) [with _Tp = std::string, _Alloc =
std::allocator<std::string>]


and one that takes two iterators.

Thanks for the replys guys. I apologize for not RTFM, but the book I am
reading is not overly detailed, and I have been trying to google most of
my questions(which usually works). Anyways, thanks for clearing this up
for me.

-Jonathan
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,780
Messages
2,569,608
Members
45,241
Latest member
Lisa1997

Latest Threads

Top