vecotr working for .erase but not .pop_back

in reference to:userMain.BookingsVector.pop_back(userMain.BookingsVector.begin()+book_name_select);


Error 2 error C2660: 'std::vector<_Ty>::pop_back' : function does not take 1 arguments f:\project3.1\project3.1\project3.1.cpp 222 1 project3.1

any suggestions?


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
class Bookings{
public:
	Bookings();
	string name;
	void display_name();
};
class Users{
public:
	Users();
	vector<Bookings> BookingsVector;
	string username;
};
Bookings::Bookings(){
};

Users::Users(){
};
int main(){

Bookings::display_name{
cout<<name<<endl;
};


case 2:{

					std::cout<<"Welcome to the edit booking menu!"<<endl
					<<"select booking"<<endl;
					
	
	while(loop <userMain.BookingsVector.size()){
	
		cout<<"["<<loop<<"]";
		userMain.BookingsVector[loop].display_name();
	
	
	loop=loop+1;}
	cin>>book_name_select;
	userMain.BookingsVector.pop_back(userMain.BookingsVector.begin()+book_name_select);

Last edited on
http://www.cplusplus.com/reference/stl/vector/pop_back/

As you will see above, pop_back doesn't accept any arguments, instead it just deletes the very last element in a container, no matter where the iterator is, or in your case where the pointer arithmetic puts it.
I'm just guessing he meant to use push_back() instead of pop_back() since he's taking user input right before the call...
He's using pointer arithmetic on that input, though, which indicates that he's trying to delete a certain element based on the beginning of the vector plus however many entries the user indicates, potentially by choosing a book that was displayed in the loop for displaying the names of the books already contained in the vector.
Sometimes I feel like a goat staring at a watch...
cipher is right, i'm trying to have the user enter the selected book out of the ones displayed then it deletes that specific booking, when i do .erase it erases all of the bookings, not just the user entered one.

is there another function of vector i can use? or is there a way to do what i said above?
Last edited on
when i do userMain.BookingsVector.begin()+book_name_select;
userMain.BookingsVector.pop_back;

EDIT:
it deletes all of them from displaying(caught that it still is viable to enter).....




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
class Bookings{
public:
	Bookings();
	string name;
	void display_name();
};
class Users{
public:
	Users();
	vector<Bookings> BookingsVector;
	string username;
};
Bookings::Bookings(){
};

Users::Users(){
};
int main(){

Bookings::display_name{
cout<<name<<endl;
};


case 2:{

					std::cout<<"Welcome to the edit booking menu!"<<endl
					<<"select booking"<<endl;
					
	
	while(loop <userMain.BookingsVector.size()){
	
		cout<<"["<<loop<<"]";
		userMain.BookingsVector[loop].display_name();
	
	
	loop=loop+1;}
	cin>>book_name_select;
userMain.BookingsVector.begin()+book_name_select;
	userMain.BookingsVector.pop_back;
Last edited on
bump
.erase() let's you specify a position to erase via an iterator:
MyVector.erase(MyVector.begin()+PositionToErase);
http://www.cplusplus.com/reference/stl/vector/erase/
Last edited on
when i do that it is still viable to enter the position, but the display doesnt show the
[0] bookname
[1] bookname2

etc.

it just shows welcome to the edit booking menu, please select booking with no options even though you can still enter a position
bump
Because the vector is now empty? Or because loop < size is not longer true? I'm not sure what you mean.
the vector isnt empty, it isnt the loop<size, it just doesnt display the bookings that are in there once you remove one booking; however it is still viable to enter 0,1,2....etc.
okay i figured out if you remove the booking, then add a new one, it will show t newly added booking, but not the previous ones. i'm confused
bump
Your code is terribly hard to read. That's why nobody wants to help you with it. You need an iterator, and the User class is useless.

http://www.cplusplus.com/reference/std/iterator/

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
#include <iostream>
#include <vector>

class Bookings{
public:
    Bookings(){}; // Empty constructors should be defined in-line.
    ~Bookings(); // Define a constructor, define a destructor.
    string name;
    void display_name();
};

void Bookings::display_name() // Put a return type on your functions
                              // and put them outside of main.
{
    std::cout << name << endl;
}

int main()
{
    vector<Bookings> BookingsVector; // Vector in main.
    vector<Bookings>::iterator it; // Iterator for the vector.

    std::cout << "Welcome to the edit booking menu!" << endl << "select booking" << endl;
    for (int i = 0; i < BookingsVector.size(); ++i){ // For loop does what your
                                                     // while loop does, but cleaner.
        std::cout << "[" << i << "]"; 
        BookingsVector[i].display_name();
    }
    std::cin >> book_name_select; // Get user input.
    it = BookingsVector.begin()+book_name_select; // Set the iterator.
    it.erase(); // Erase the record.
}
Last edited on
Topic archived. No new replies allowed.