3

I have an object that contains an STL vector. I start with the vector being size zero and use push_back to add to it. So, push_back works fine.

In my code, each element in the vector represents an atom. Thus the object this STL vector is inside is a "molecule".

When I try to remove an atom from my molecule i.e., erase one of the elements from the array, the erase() function does not work. Other methods do work such as size() and clear(). clear() removes all elements though, which is overkill. erase() is exactly what I want, but it doesn't work for some reason.

Here is an incredibly simplified version of my code. It does, however, represent the problem exactly.


#include <iostream>
#include <vector>
#include <string>
#include <algorithm>

using namespace std;

class atomInfo 
{
/* real code has more variables and methods */
public:
    atomInfo () {} ;
};

class molInfo 
{
/* There is more than 1 atom per molecule */
/* real code has more variables and methods */
public:
    vector <atomInfo> atom;
    molInfo () {};
};

int main ()
{
    int i;
    molInfo mol;

    for( i=0; i<3 ; i++)
        mol.atom.push_back( atomInfo() );
    //mol.atom.clear() ; //Works fine
      mol.atom.erase(1) ; //does not work
}    

I get the following error when I use erase():

main.cpp: In function ‘int main()’: main.cpp:39:21: error: no matching function for call to ‘std::vector::erase(int)’ mol.atom.erase(1) ;

JFMR
  • 23,265
  • 4
  • 52
  • 76
Charlie Crown
  • 1,071
  • 2
  • 11
  • 28
  • 1
    This will not work even if it's not "inside a structure object". This has absolutely nothing to do, whatsoever, with the vector being inside an object, or not. `erase()` does not take an `int` parameter. `erase()` takes an iterator as a parameter. See your C++ book for more information. – Sam Varshavchik Dec 31 '18 at 00:46
  • Which overload of vector's erase member are you expecting that to use? https://en.cppreference.com/w/cpp/container/vector/erase – Shawn Dec 31 '18 at 00:47
  • my book says name.erase(pos) "Removes the element at the specified position" My mistake for assuming position was given by an integer, I will look into this more and delete the question after. – Charlie Crown Dec 31 '18 at 00:48
  • 1
    Hint: mol.atom.begin() is an iterator to the first item ( if the vector is not empty). – drescherjm Dec 31 '18 at 00:49
  • Does the book not mention what `pos` means? Something like `iterator erase( const_iterator pos );`? – Baum mit Augen Dec 31 '18 at 00:51
  • Yes, it is. We expect people to read their C++ book, from cover to cover, before they attempt to post questions here on a subject matter that's surely covered in the book. stackoverflow.com is not a substitute for a C++ book. – Sam Varshavchik Dec 31 '18 at 00:51
  • 1
    Remember votes for a question depend on how much we think it will be useful to help future readers. – drescherjm Dec 31 '18 at 00:52
  • my book only gives the one line on the erase(), and I mistook pos for meaning an integer. But fair enough, it is what it is. – Charlie Crown Dec 31 '18 at 00:53
  • 2
    I will of course delete it, but you guys did help, so thankyou for your time, downvotes and all – Charlie Crown Dec 31 '18 at 00:54
  • 3
    Good MCVE, good effort making the question, even if it was a simple rookie mistake. You probably got the downvotes for not following up on the obvious error message by looking at the docs for that function. I'll give you a +1 for making the problem clear and reproducible, which is what we're here for. It will be useful if future posters make such clear demonstrations. – Kenny Ostrom Dec 31 '18 at 00:54
  • 2
    Perfect MCVE though, good job with that. Too rare. Keep it up! – Lightness Races in Orbit Dec 31 '18 at 00:57

1 Answers1

5

It looks like you thought std::vector::erase took an index from the start of the container.

It's not clear where you got this idea from, as it is not what the documentation says.

These functions work with iterators.

Fortunately, with vectors, you can get the effect you want by adding a number to an iterator.

Like this:

mol.atom.erase(mol.atom.begin() + 1);

Said documentation in fact does have an example to this effect.

Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055