-2

I wanted to declare a 2D array like this without the size:

int weightcalls = {{0,112}, {112,115}};

But the program said that it is wrong, and that I have to put the size of the in first.

Matt C
  • 4,470
  • 5
  • 26
  • 44
  • When you said "the program said that is wrong", what do you mean? I'm sure you meant that an error occured. Please say what that error is. And also be sure to format your code. – Matt C Mar 06 '16 at 20:48

3 Answers3

1

You can't declare an array without specifying its size in C++, you always have to do something like this int myarray[2];, if you need a dynamic array, if you don't know the array's size at compile-time, then you have to use a pointer like this int* myarray; or some container from the STL, for example std::vector, wich is widely used and really easy to use:

#include <vector>

int main(int argc, char* argv[]) {

std::vector<int> myvector;
myvector.resize(2); //Set the size of the vector to be 2
myvector.push_back(1);
myvector.push_back(2);

return 0;
}

Using C++, it is preferred to use std::vector over a pointer. Hope I helped you.

Matth
  • 154
  • 7
0

Try using vectors instead.

For example

#include <vector>
using namespace std;

int main(){
    vector< vector <int> > v;
}

You can then resize the vector to set the dimensions e.x

v.resize(2); //This will make a 2D vector

and then you can resize it later (or just use "push_back") to push back elements.

e.x

v[0].resize(2);
//This will make the first "row" of the vector to have 2 empty places
v[0].push_back(6);
v[0].push_back(10);
//This will push back the number 6 as the first element of the first law.

at the first example the vector should look like this

0 0
0

Were 0s show empty places that can be filled with elements and at the second one like this

6 10
0

I'm sorry if my explanation wasn't that detailed. I'm new here. Hope it helps though. I highly recommend using vectors in general as they are much more flexible.

Oh and you may also want to check out this post (it might help) C++ 2D vector and operations

Community
  • 1
  • 1
Panos
  • 1,764
  • 21
  • 23
0

As Panos said you could declare the array as a vector. This would give you the ability to resize and add to your array later on as well. However if you simply wanted to declare the array and use it as such, then I believe you could use:

int weightcalls[][] = {{0, 112}, {112, 115}};

or you could declare it as a pointer:

int ** weightcalls = {{0, 112}, {112, 115}};

but if you use it as a pointer, then you probably want to keep the inner size as two to lessen confusion later. Also, you will want to make sure you fully understand pointers and memory placement in c++ to ensure you don't cause a memory issue (lack of specificity here because all memory problems, in general, are bad).

One reason you would not want to use vectors though is size and portability. Many versions of c++ on smaller systems and micro-controllers (i.e. Arduino) do not have "direct" support for vectors. Although some people have made support for vectors for these systems, it still takes up a large amount of space.

Dylan Turner
  • 322
  • 1
  • 12