Problem erasing a vector elements

Nov 12, 2014 at 9:22am
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?
Nov 12, 2014 at 9:38am
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.
Nov 13, 2014 at 2:28pm
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 Nov 13, 2014 at 2:28pm
Nov 13, 2014 at 2:40pm
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.
Nov 13, 2014 at 3:55pm
Could you provide an example, please?
Nov 13, 2014 at 6:58pm
help
Nov 13, 2014 at 7:30pm
Do you know how to search for an element in a vector?
Last edited on Nov 13, 2014 at 7:30pm
Nov 21, 2014 at 11:05am
@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

Nov 21, 2014 at 11:19am
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;
Nov 21, 2014 at 11:46am
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
Nov 21, 2014 at 12:40pm
Your << operator for outputting obj instances is not written correctly
Nov 24, 2014 at 5:13pm
Could someone, please, write for me the correct function to remove the object?

void CStudent::removeObject(const Object& item)
{
objects.erase(object.begin() + item);
}
Nov 24, 2014 at 5:32pm
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.
Nov 24, 2014 at 5:37pm
Thank you for the reply.

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

Nov 24, 2014 at 6:32pm
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
Nov 24, 2014 at 6:53pm
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.

Nov 24, 2014 at 6:54pm
Line 6 can never be reached because you return from the function on line 5.
Nov 24, 2014 at 7:07pm
yup, you're right
Topic archived. No new replies allowed.