0

So I'm trying to create a 2D array, I'm new to C++ so I'm a little confused, I know I am using too many new operators but I don't know which one to keep. And I'm not sure what to do about the operator error.

Node class -

class Node
{
 public:
  int name;
  Node *topedge;
  Node *bottomedge;
  Node *leftedge;
  Node *rightedge;
};

Main

#include <iostream>
#include <node.h>
#include <cstdlib>
using namespace std;
int main(void)
{
    Node* grid = new Node[10][10]; //error here
                                                                                
  //populate grid                                                                                             
  int h = 0;
  for ( int j = 0; j < 10; j++ ){
    for ( int i = 0; i < 10; i++ ){
      grid[j][i] = new Node(); //error here
      grid[j][i]->name = h; //error here
      h++;
     }
  }
  for ( int j = 0; j < 10; j++ ){
    for ( int i = 0; i < 10; i++ ){
      cout << grid[j][i]->name << '('<< j << ',' << i << ')' << endl;
    }
  }

  return 1;

}

The following is the errors I get when running it,

  main.cpp: In function ‘int main()’:
    main.cpp:10:33: error: cannot convert ‘Node (*)[10]’ to ‘Node*’ in initialization
         Node* grid = new Node[10][10];
                                     ^
    main.cpp:17:14: error: no match for ‘operator[]’ (operand types are ‘Node’ and ‘int’)
           grid[j][i] = new Node();
                  ^
    main.cpp:18:14: error: no match for ‘operator[]’ (operand types are ‘Node’ and ‘int’)
           grid[j][i]->name = h;
                  ^
    main.cpp:25:22: error: no match for ‘operator[]’ (operand types are ‘Node’ and ‘int’)
           cout << grid[j][i]->name << '('<< j << ',' << i << ')' << endl;
                          ^
    <builtin>: recipe for target 'main.o' failed
Lauren
  • 11
  • 3
  • 2
    `new Node[10][10];` doesn't return a `Node *`;. Since you know the size in advance, can I talk you into a nice `std::array`? – user4581301 Sep 11 '20 at 17:32
  • Probably, I just don't know how to do that, I just need to make sure my references work and nodes exist – Lauren Sep 11 '20 at 17:38
  • 1
    `std::array, 10>, 10> grid;` Is a 10x10 array of `Nodes`. No need for `grid[j][i] = new Node();` because the `Node`s are already there. [Documentation for `std::array`](https://en.cppreference.com/w/cpp/container/array) – user4581301 Sep 11 '20 at 17:40
  • 1
    If `std::array` is not available, `Node grid[10][10];` will do almost the same thing. It's just [more annoying to pass to functions](https://stackoverflow.com/questions/1461432/what-is-array-to-pointer-decay). – user4581301 Sep 11 '20 at 17:43
  • My apologies, I have a typo above. `std::array, 10>, 10> grid;` should have been `std::array, 10> grid;` – user4581301 Sep 11 '20 at 17:46
  • I don't plan on passing it to any functions, because I don't is there a way to use the Node grid[10][10]; (I'm just more used to it coming from java) – Lauren Sep 11 '20 at 17:49
  • What about `Node grid[10][10];`? You don't need any `new` here. To assign nodes, you could do `grid[i][j] = Node(/* whatever */);` (assuming `Node` provides an assignment) or just access the existing nodes `grid[i][j].name = h;` Btw. you didn't expose how `Node` is defined. – Scheff's Cat Sep 11 '20 at 17:51
  • This worked! thank you! Also what do you mean, I should've posted the Node class? I can edit It to include that – Lauren Sep 11 '20 at 17:56
  • Try not to use too much Java thinking in C++. For example, the more you use `new` in a program, the worse things get. In Java you have an infinite memory simulator (the garbage collector tries to hide the fact that you don't have infinite memory) and just grab objects and discard them when you're done. You can't think that way in C++. Everything needs to eb accounted for in a complicated program. – user4581301 Sep 11 '20 at 17:57
  • You're right, I'm just getting used to understanding how memory works In C++ but I'll make sure I'm more careful – Lauren Sep 11 '20 at 18:00

2 Answers2

0

First define your class Node then try this

Node** grid = new Node*[10];
for(int i = 0; i < 10; ++i)
    grid[i] = new Node[10];
Hridoy_089
  • 190
  • 2
  • 8
0

A sample example with std::array. Explanatory comments inserted where I saw a need.

#include <iostream>
#include <array>
#include <string>
// using namespace std; prefer not to use this. It can cause confusing bugs


// simple test node
struct Node
{
    std::string name;
};

int main() // no need for void C++ knows when a parameter list is empty by
           // the absence of parameters.
{
    std::array<std::array<Node, 10>, 10> grid;

    // give the nodes names for testing purposes
    for (size_t j = 0; j < grid.size(); j++) // using size of grid instead of
                                             // magic number
    {
        for (size_t i = 0; i < grid[i].size(); i++)
        {
            grid[j][i].name = std::to_string(j) + "," + std::to_string(i);
        }
    }

    // print the names
    for (size_t j = 0; j < grid.size(); j++)
    {
        for (size_t i = 0; i < grid[i].size(); i++)
        {
            std::cout << grid[j][i].name << '(' << j << ',' << i << ')' << std::endl;
        }
    }

    return 1;

}

Documentation for std::array

Recommended reading:

Why is “using namespace std;” considered bad practice?

Why should C++ programmers minimize use of 'new'?

user4581301
  • 29,019
  • 5
  • 26
  • 45