Code Not Working (Vectors)
Posted: 01 Jan 2020, 13:01
As a test, I wrote a code sample that includes a pointer to a vector in a struct. It attempts to create an instance of the struct, assign a new vector to it, assign a value to one of the vector's members, and then print that value as read from the vector.
However, when I run the code, I get an error "vector subscript out of range". Am I doing something wrong?
Code: Select all
#include <iostream>
#include <vector>
using namespace std;
typedef struct {
__int64 size = 0;
vector<__int64>* array;
} int_array;
int main () {
int_array num_array;
num_array.array = new vector<__int64>;
num_array.array->reserve(5);
(*num_array.array)[0] = 5;
cout << (*num_array.array)[0];
return 0;
}