Doing a pushback with an array pushback value is not showing

Hi I'm trying to do a pushback in a function with an array. Not quite sure why the pushback value is not displaying in the console. Here's part of the code.
Header function
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
  template <class TBD> 
class TArray {
private:
	TBD *myArray;
	int size;
public:
	TArray(int size = 0) {
		if (size < 0) {
			throw invalid_argument("invalid size");
		}
		myArray = new TBD[size];
		this->size = size;
	}
	~TArray() {
		delete[] myArray;
	}
	void set(TBD value, int index) {
		if (index < 0 || index >= size) {
			throw invalid_argument("out of range");
		}
		myArray[index] = value;
	}
	TBD get(int index) {
		if (index < 0 || index >= size) {
			throw invalid_argument("out of range");
		}
		return myArray[index];
	}
	int getSize() {
		return size;
	}
	void push_back(TBD value) {
		if (size >= 0) {
				cin >> int;
				myArray = new TBD[size+1];
				myArray.pushback(int);
				return size;
		}
		else {
			cout << "please enter a valid number." << endl;
		}


Main
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
if (choice == 1) {
		std::cout << "What value do you want to add onto the end of the list?" << endl;
		numPlayers = numPlayers + 1;

		std::cin >> value;
		myValues.push_back(value);
		for (int i = 0; i < myValues.getSize(); i++) {
			std::cout << "value " << i << " = " <<
				myValues.get(i) << endl;
		}
	}
	else {
		cout << "Thanks for playing." << endl;
	}


I'm trying to figure this out for a course, but not really sure what I missed, I looked at the section for the book "C++ programming" (Malik) and the prof said it is easy to do, but I've spent too long on it trying to research and figure out my mistake and I'm still confused on it. If anyone could help I would immensely appreciate it.
1
2
myArray = new TBD[size+1];
myArray.pushback(int);
This shouldn't even compile. Also is it pushback or push_back? Even if it did compile, it would appear you would be recursively calling push_back, which is probably not good.

When creating a dynamic array, one thing you might want to do is differentiate between capacity and size. The current "size" of an array might only be 2, as far as the user is concerned, but there might be allocated space for up to 10 items, for example. If the user then tries to push_back an 11th item, then the array needs to be re-allocated with a larger size.

However, in the simplest case (but not the efficient case), you would re-allocate the dynamic array each time you push_back, and copy over the old elements; so there is no difference between size and capacity.

Something like:
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
// Example program
#include <iostream>

// not a template, just for simplicity.
// what's important is to understand the push_back function
class DynamicArray {
  public: // for simplicity
    int* arr = nullptr;
    int size = 0;
    
    void push_back(int value)
    {
        // allocate a new array that is 1 larger than the current array
        int* new_arr = new int[size + 1];
        
        // copy over old elements from current array
        for (int i = 0; i < size; i++)
        {
            new_arr[i] = arr[i];   
        }
        
        // copy the new element into the new array
        new_arr[size] = value;
        
        // increase the new size
        size++;
        
        // clean up the old memory
        delete[] arr;
        
        // point the array to the new memory now
        arr = new_arr;
    }
    
    ~DynamicArray()
    {
        delete[] arr;   
    }
    
    int& operator[](int index)
    {
        return arr[index];   
    }
};

int main()
{
    DynamicArray arr;
    
    arr.push_back(42);
    arr.push_back(123);
    arr.push_back(1729);
    
    for (int i = 0; i < arr.size; i++)
    {
        std::cout << arr[i] << '\n';   
    }

}

42
123
1729


My example is only the bare minimum to show an example of re-allocating an array to a bigger size.
Last edited on
Thanks Ganado, you are a lifesaver! I have no idea why it still compiles. That's probably what is confusing me the most because it makes me think I just need to adjust the code rather than rewrite portions.

I like that your comments consistently allow me to think differently about the code, so that I can better understand how the code is being read by the compiler.
Last edited on
Topic archived. No new replies allowed.