0

In java, multidimensional arrays of objects diclared like this (A is type of object):

A[][] array = new A[5][5];

for(int i = 0;i<5;i++){
    for(int j = 0;j<5;j++){
        array[i][j] = new A();
    }
}

how can I do the same in C++?

8
  • You can use a std::vector<std::vector<A>> to do the same in c++. Commented Dec 24, 2016 at 9:42
  • Possible duplicate of How do I use arrays in C++? Commented Dec 24, 2016 at 9:42
  • Its not the same.... I need to make object array and not variable array (e.g. float or integer) Commented Dec 24, 2016 at 9:48
  • @gal What do you think is the difference? There isn't any. Commented Dec 24, 2016 at 11:04
  • 1
    Don't use multi-dimensional arrays in C++ (or in C). Commented Dec 24, 2016 at 14:41

3 Answers 3

1

Another idea for a multi dimensional array is if you use std::vector

#include <vector>

class A{
//Set properties here
};

int main(){

   //Init vector
   std::vector<std::vector<A>> array;

   std::vector<A> tempVec;

   for(int i = 0;i<5;i++){

       for(int j = 0;j<5;j++){
           A aValue;

           //Set properties for object A here

           tempVec.push_back(aValue);
       }
       array.push_back(tempVec);
   }
}

The good thing about a vector is that there is no limit to the amount of items;

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

Comments

1

Unless I have misunderstood your question in some way, to declare a two-dimensional array in C++ you could use this:

A variable; // Declares a variable of A type, named variable
A array[5][5] = {{ variable, variable, variable, variable, variable },
                        { variable, variable, variable, variable, variable },
                        { variable, variable, variable, variable, variable },
                        { variable, variable, variable, variable, variable },
                        { variable, variable, variable, variable, variable }};

If you think of a two-dimensional array as a virtual table, you just declare the values by row, each row is a set of curly brackets, then surround the whole table with a final set of brackets.

If you are in love with for loops you can still use them:

A variable;
A array[5][5];
for (int row = 0; row < 5; row++){
    for (int col = 0; col < 5; col++){
        array[row][col] = variable;
    }
}

Comments

0

You can easily use the code like this:

A array[5][5];

It will create the 2D array and it will initialize each cell with A object. This piece of code equals to the code in Java like this:

A[][] array = new A[5][5];

for(int i = 0;i<5;i++){
    for(int j = 0;j<5;j++){
        array[i][j] = new A();
    }
}

Full code which works properly:

class A{};
int main() {
    A array[5][5];
    return 0;
}

9 Comments

Its the same code I wrote in java.... anyway, when I compile this code i get an error:
Its in another file. I imported it in the start of the main file.
Array creates properly, the problem is with your code. Please, post your code, or create another question with the problem you have now.
The problem is that I can't create the objects like this: array[i][j] = new A(); Its says: error: no match for 'operator=' (operand types are 'A' and 'A*')
@gal Stop using new in c++. It's wrong in most cases.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.