remove_last function help.

So i need a remove_last function that takes out the last thing added to the array. (All you need to do for this is to decrement the used counter. You do not need to change the capacity). Will this work?

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

class Numbers
{
 	public:
                Numbers();
		void insert(int item);
		void resize();
		void display();
		Numbers(const Numbers & other);
		void operator = (const Numbers& other);
	private:
		unsigned long * data;
		std::size_t used;
		std::size_t capacity;
		
};

Numbers::Numbers(){
	used = 0; capacity = 5;
	data = new int [capacity];
	
}
void Numbers::insert(int item){
	if (used == capacity)resize();
	data[used]=item;++used;
}
void Numbers::resize(){
	unsigned long * tmp;
	tmp = new int [capacity +5];
	copy(data, data+used, tmp);
	capacity += 5;
}
void Numbers::remove_last(){
        delete []data;
	data=tmp;
}
Last edited on
(All you need to do for this is to decrement the used counter. You do not need to change the capacity). Will this work?
It should but why don't you try ?
I have tried but I`m getting a lot of errors. Such as no 'void Numbers::remove_last()' member function declared in class 'Numbers'. Do i have to have member functions?
Last edited on
Create a simple test program with a main function to test your class and post all error messages here.
I fixed remove_last now I have other errors in my program to find lol.
Topic archived. No new replies allowed.