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