473,396 Members | 2,020 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

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 1983
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

29
by: Hagen | last post by:
Hello, in a recent thread "speed of vector vs array" I read about the problem of the slow acces by addressing vector elements by indexing, unfortunately I see no workaround in my case. My...
7
by: William Payne | last post by:
(This post is related to "recent files menu"-post below. If I should have kept this in that thread, I apologise.) Hello, I have a function that adds a std::string to a std::vector. New entries are...
34
by: Adam Hartshorne | last post by:
Hi All, I have the following problem, and I would be extremely grateful if somebody would be kind enough to suggest an efficient solution to it. I create an instance of a Class A, and...
6
by: Roman Töngi | last post by:
I want to change a vector in a function. I pass a pointer of it to the function and append an item. Then I want to print the first item in the vector. It doesn't work. Can anyone help me? Thanks...
10
by: eiji | last post by:
Hi folks, I have a problem compiling this under VC6! Maybe someone has some minutes to look at this: template<class T> class Compare { public: Compare(){}; virtual ~Compare(){};
3
by: eriwik | last post by:
I use the following structure to store filenames for one or more "sets" grouped together by a number: map<int, map<string> > fileSets; As arguments to the constructor I send a...
4
by: arnuld | last post by:
i wrote a programme to create a vector of 5 elements (0 to 4), here is the code & output: #include <iostream> #include <vector> int main() { std::vector<intivec; // dynamically create a...
3
by: PolkaHead | last post by:
I was wondering if there's a way to traverse a two-dimensional vector (vector of vectors) with a nested for_each call. The code included traverses the "outer" vector with a for_each, than it...
5
by: Alan | last post by:
I was wondering whether it is good programming practice or asking for trouble to modify a vector while iterating through it. That is, I want to do something like the following pseudocode in C++: ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.