473,796 Members | 2,559 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.t xt", 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.c pp" -o "C:\C++\notes.e xe" -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:64 5: candidates are:
__gnu_cxx::__no rmal_iterator<_ Tp*, std::vector<_Tp , _Alloc> >
std::vector<_Tp , _Alloc>::erase( __gnu_cxx::__no rmal_iterator<_ Tp*,
std::vector<_Tp , _Alloc> >) [with _Tp = std::string, _Alloc =
std::allocator< std::string>]

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

__gnu_cxx::__no rmal_iterator<_ Tp*, std::vector<_Tp , _Alloc> >
std::vector<_Tp , _Alloc>::erase( __gnu_cxx::__no rmal_iterator<_ Tp*,
std::vector<_Tp , _Alloc> >, __gnu_cxx::__no rmal_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 1997
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.t xt", 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.c pp" -o "C:\C++\notes.e xe" -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:64 5: candidates are:
__gnu_cxx::__no rmal_iterator<_ Tp*, std::vector<_Tp , _Alloc> >
std::vector<_Tp , _Alloc>::erase( __gnu_cxx::__no rmal_iterator<_ Tp*,
std::vector<_Tp , _Alloc> >) [with _Tp = std::string, _Alloc =
std::allocator< std::string>]

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

__gnu_cxx::__no rmal_iterator<_ Tp*, std::vector<_Tp , _Alloc> >
std::vector<_Tp , _Alloc>::erase( __gnu_cxx::__no rmal_iterator<_ Tp*,
std::vector<_Tp , _Alloc> >, __gnu_cxx::__no rmal_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.b egin() + 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.t xt", 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.c pp" -o "C:\C++\notes.e xe" -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:64 5: candidates are:
__gnu_cxx::__no rmal_iterator<_ Tp*, std::vector<_Tp , _Alloc> >
std::vector<_Tp , _Alloc>::erase( __gnu_cxx::__no rmal_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:66 8:

__gnu_cxx::__no rmal_iterator<_ Tp*, std::vector<_Tp , _Alloc> >
std::vector<_Tp , _Alloc>::erase( __gnu_cxx::__no rmal_iterator<_ Tp*,
std::vector<_Tp , _Alloc> >, __gnu_cxx::__no rmal_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.t xt", 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.c pp" -o "C:\C++\notes.e xe" -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:64 5: candidates are:
__gnu_cxx::__no rmal_iterator<_ Tp*, std::vector<_Tp , _Alloc> >
std::vector<_Tp , _Alloc>::erase( __gnu_cxx::__no rmal_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:66 8:

__gnu_cxx::__no rmal_iterator<_ Tp*, std::vector<_Tp , _Alloc> >
std::vector<_Tp , _Alloc>::erase( __gnu_cxx::__no rmal_iterator<_ Tp*,
std::vector<_Tp , _Alloc> >, __gnu_cxx::__no rmal_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
3986
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 case: class A {
7
12636
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 added at the front (index 0) of the vector. If the vector contains a certain amount of elements, the element at the back is removed when a new one is added. If one tries to add a string already stored in the vector, that string is supposed to...
34
4177
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 "push_back" a copy of this into a vector V. This is repeated many times in an iterative process. Ok whenever I "push_back" a copy of Class A, I also want to assign a pointer contained in an exisiting instance of a Class B to this
6
7470
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 #include <vector> #include <iostream> #include "element.h" using namespace std;
10
4091
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
5856
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 vector<vector<string> > where each vector<string> contains the filenames of one set. The function getNumber() calculates a number from the filename. The constructor then looks like this:
4
5709
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 vector
3
2887
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 relies on the PrintFunctor to traverse the "inner" vector with another for_each. Is it possible to nest the second for_each, so I don't have to include a for_each in my function object. Is it possible to do a: for_each(myVec.begin(), myVec.end(),
5
12158
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++: for each element of the vector for each subsequent element of the vector if the belong together <some code to compare them> then merge the elements (add the 2nd to the 1st, then delete the 1st)
0
9673
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9525
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10452
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10221
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10169
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10003
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6785
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5569
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3730
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.