Problem erasing a vector elements

Hello everyone,

i'm having a problem erasing a vector element.
The error i get is the following:
"no match for 'operator+' in '((CStudent*)this)->CStudent::objects.std::vector<_Tp, _Alloc>::begin<Object, std::allocator<Object> > () + item"

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
  #include <iostream>
#include <vector>
#include <string>

using namespace std;
struct Object {
        string name;
        string description;
};

class CStudent{

  public:
    string _name, _genre="";
    std::vector<Object> objects;
    
    CStudent (string,string, int, bool, int);
    string getName () {return (_name);}

    void addObject(const Object& item);
    void removeObject(const Object& item);

};

void CStudent::addObject(const Object& item)
{
    objects.push_back(item);
}

void CStudent::removeObject(const Object& item)
{
    objects.erase(object.begin() + item);
}

//CStudent::CStudent()
//{
//
//}

CStudent::CStudent (string name, string) {
  _name=name;
  _genre = genre;  
}


Any hint?
The erase function takes an iterator to the element that should be removed as argument.

object.begin() returns an iterator to the first element in the vector. By adding an index you get an iterator to the element at that index position. Example: object.begin() + 3 gives you an iterator to object[3].

item is not an index. You probably will have to search the vector to find the position before calling erase.
looking at the example, here (http://www.cplusplus.com/reference/vector/vector/erase/), it seems that i can directly do:

1
2
3
4

objects.erase(object.begin() + item);

Last edited on
Yes. As long as item is index of an object and not an object itself.

If you want to erase a specific object, you need to find it in vector first.
Could you provide an example, please?
help
Do you know how to search for an element in a vector?
Last edited on
@Peter87
at this point i have to say that i don't really know how to search throug a vector.

i tried with iterators, by index.... nothing

If you use a loop you can put an if statement inside the loop to test if the item is equal to the item that you want to erase. When you have found the item you pass the iterator (or the object.begin() + index) to the erase function. After you have done that you make the loop stop by using break;
i tried:

here's what i've got:

std::vector<obj>::iterator it;
for(it = CharacterObject.begin(); it != CharacterObject.end(); ++it) {
std::cout << (*it) << '\n';
}

error: cannot bind 'std::ostream {aka std::basic_ostream<char>}' lvalue to 'std::basic_ostream<char>&&'|

this using iterator
Your << operator for outputting obj instances is not written correctly
Could someone, please, write for me the correct function to remove the object?

void CStudent::removeObject(const Object& item)
{
objects.erase(object.begin() + item);
}
1
2
auto it = std::find(objects.begin(), objects.end(), item); //Find object iterator 
objects.erase(it); //Erase it 
Handle situation when object is not found in vector yourself.
Thank you for the reply.

What about if i want to remove the object by name and not by an index?

What about if i want to remove the object by name and not by an index?
You are not removing element by index. You are removing element (single element) which equals to passed one. Use find_if:
http://en.cppreference.com/w/cpp/algorithm/find
To find element satisfying specific condition instead.

If there might be several element which needs to be removed, use remove/remove_if:
http://en.cppreference.com/w/cpp/algorithm/remove
thank you

i used this in the end:

1
2
3
4
5
6
7
8
9
    for (int x = 0; x != objects.size(); ++x)
    {
        if (objects[x].name==name) {
         objects.erase (objects.begin()+x);
        return -1;
        break;
        }
    }


Don't know why used various example of iterators haven't worked.

Line 6 can never be reached because you return from the function on line 5.
yup, you're right
Topic archived. No new replies allowed.