I'm trying to make a stack of integer arrays, like so:
stack<int[2]> stk;
int arr[2] = {1,2};
stk.push(arr);
however, Visual C++ gives me the error
error C2075: 'Target of operator new()' : array initialization needs curly braces
and MinGW gives me the error
error: parenthesized initializer in array new
The error seems to be coming from stk.push(arr). What does the error mean, and how would I properly make a stack of integer arrays?
With C++11 arrays you can do this:
#include <stack>
#include <array>
stack<array<int, 2>> arrs;
arrs.push({1, 2});
As mentioned in a comment to the question, it is also possible to replace array<int, 2> with vector<int>. However, array<int, 2> achieves what you where describing with a fixed size container (and lower memory usage.)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With