3

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?

2
  • 1
    You can't use raw arrays in containers. For one, they're not copyable/moveable. Commented Feb 27, 2014 at 19:06
  • stack<vector<int> > stk; Commented Feb 27, 2014 at 19:07

1 Answer 1

3

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.)

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.