469,646 Members | 1,367 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,646 developers. It's quick & easy.

Erasin Vector Element

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::out);
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
Jul 22 '05 #1
3 1717
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.
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::out);
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
Jul 22 '05 #2
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::out);
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<std::string, std::allocator<std::string>
>::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.

Jul 22 '05 #3
Rolf Magnus wrote:
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::out);
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<std::string, std::allocator<std::string>
>::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
Jul 22 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

29 posts views Thread by Hagen | last post: by
7 posts views Thread by William Payne | last post: by
34 posts views Thread by Adam Hartshorne | last post: by
6 posts views Thread by Roman Töngi | last post: by
10 posts views Thread by eiji | last post: by
3 posts views Thread by eriwik | last post: by
4 posts views Thread by arnuld | last post: by
3 posts views Thread by PolkaHead | last post: by
reply views Thread by gheharukoh7 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.