Error with erasing a vector entry

Hey guys,

So I'm basically writing an inventory program that allows the user to interact with it. The code seems to work but I'm having a problem removing specific elements from the vector without using pop_back.

I know that removing entries in between the end and the start can invalidate entries but I'm not to sure as too how else I can do that whilst maintaining item validation. Should I use a map?

I get an error using erase and passing the item requested for removal, any ideas?

Thanks.

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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
  // Test - Display inventory

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

using namespace std;

class Invent {
public:
	void Invent::displayInventory();
	void Invent::addItem(string item);
	void Invent::removeItem(string item);
private:
	vector<string> Inventory;
	vector<string>::const_iterator iter;
};

int main() {

	Invent invent;
	unsigned int userChoice;
	bool quit = false;

		cout << "\nWelcome to your item inventory" << endl;

	while (quit == false) {
		cout << "\n1. Display inventory" << endl;
		cout << "2. Add item" << endl;
		cout << "3. Remove item" << endl;
		cout << "4. Quit\n" << endl;
		cout << "Option: ";
		cin >> userChoice;

		string item = "";

		switch (userChoice) {
		case 1:
			invent.displayInventory();
			break;
		case 2:
			cout << "\nPlease type the item name: ";
			cin.get();
			getline(cin, item);
			invent.addItem(item);
			break;
		case 3:
			cout << "\nPlease type the item name to remove: ";
			cin.get();
			getline(cin, item);
			invent.removeItem(item);
			break;
		case 4:
			quit = true;
			break;
		default:
			cout << "\nYou entered an incorrect value!\n";
			break;
		}
	}

     return 0;
}

void Invent::displayInventory() {
	if (Inventory.empty()) {
		cout << "\nSorry but your inventory is currently empty.\n";
	}
	else {
		cout << "\nDisplaying all the current items in your inventory.\n" << endl;
		for (iter = Inventory.begin(); iter != Inventory.end(); ++iter) {
				cout << "- " << *iter << endl;
		}
	}
}

void Invent::addItem(string item) {
	Inventory.push_back(item);
	cout << "\nThe item \"" << item << "\" was successfully added to your inventory." << endl;
}

void Invent::removeItem(string item) {
	iter = find(Inventory.begin(), Inventory.end(), item);
	if (iter != Inventory.end()) {
		Inventory.erase(item); // ERROR something about overloaded op
	}
}
Last edited on
Sausage wrote:
I know that removing entries in between the end and the start can invalidate entries but I'm not to sure as too how else I can do that whilst maintaining item validation.
Only iterators, pointers, and references to elements get invalidated. The elements themselves are not affected. If what you said were true, the erase member function would be entirely useless!

I don't understand how you see only certain errors and not others, look at the error messages I get:
http://ideone.com/AL1pxt

What compiler are you using? If the version you are using was released more than 5 years ago, stop using it.
Last edited on
Ah thanks for clearing that up.

That doesn't make sense, I'm using Visual Studio 2013, I don't get any errors if I remove the erase member function. Then only one when I include it. Why do all the compilations from codepad, ideone and visual 2013 all differ?

No Errors:
http://i.gyazo.com/ffb4062f76963912197dc997f612b8ec.png

Single Error:
http://i.gyazo.com/7b0ffc6d6caf8947c3ab8d918d1dfe56.png

Codepad gives one error:

http://codepad.org/kPkvINSc
Last edited on
There is a typo, you meant to write "iter" and not "item".

1
2
3
4
5
6
7
void Invent::removeItem(string item) {
	iter = find(Inventory.begin(), Inventory.end(), item);
	if (iter != Inventory.end()) {
//		Inventory.erase(item); // ERROR: can't convert string to const_iterator
		Inventory.erase(iter);
	}
}

Oh crap yeah!

Thanks Cat. :D
Last edited on
1
2
3
	void Invent::displayInventory();
	void Invent::addItem(string item);
	void Invent::removeItem(string item);


You don't need to scope inside the class.

That would essentially be like doing:
1
2
3
4
5
enum colors{ colors::red, colors::blue , colors::ect.. };

//instead of

enum colors{ red, blue, ect.. };
Thanks Giblit, makes sense actually. :D
Topic archived. No new replies allowed.