I searched for a lot of resources on C++ and arrays. I learned that arrays act like pointers in c++ and I am confused on how to create a multi-dimensional array and assigning value to indexes. I usually code in Java and Python but know I am working with an Arduino and I need to learn c++.
My Arduino(c++) code regarding about this array is:
#include "Arduino.h"
#include "cell.h"
#include <cell.h>
cell maze[16][16];
cell * current = new cell(1, 1, 0, false, 0);
cell * end_pt = new cell(1,1,1,true);
maze[15][15] = end_pt;
My .h and .cpp files;
#include "Arduino.h"
#include "cell.h"
#include "Arduino.h"
cell::cell(){
right = 0;
}
cell::cell(int r, int l, int f, bool inf){
right = r;
left = l;
forw = f;
info = inf;
value = 70;
printf("%d\n", right);
printf("%d\n", left);
printf("%d\n", forw);
printf("%d\n", inf);
printf("%d\n", val);
}
cell::cell(int r, int l, int f, bool inf, int val){
right = r;
left = l;
forw = f;
info = inf;
value = val;
printf("%d\n", right);
printf("%d\n", left);
printf("%d\n", forw);
printf("%d\n", inf);
printf("%d\n", val);
}
void cell::setR(int r){
right = r;
}
void cell::setL(int l){
left = l;
}
void cell::setF(int f){
forw = f;
}
void cell::setI(bool inf){
info = inf;
}
void cell::setV(int val){
value = val;
}
int cell::getR(){
return right;
}
int cell::getL(){
return left;
}
int cell::getF(){
return forw;
}
bool cell::getI(){
return info;
}
int cell::getV(){
return value;
}
#ifndef cell_h
#define cell_h
#include "Arduino.h"
class cell{
public:
cell();
cell(int r, int l, int f, bool info);
cell(int r, int l, int f, bool info, int val);
void setR(int r);
void setL(int l);
void setF(int f);
void setI(bool inf);
void setV(int val);
int getR();
int getL();
int getF();
bool getI();
int getV();
private:
int right;
int left;
int forw;
bool info;
int value;
};
#endif
'maze' does not name a type is my error. Please help and thank you in advance!
There's a problem with this line:
maze[15][15] = end_pt;
maze[15][15]
, and any other object in maze, is of type cell
end_pt
is of type cell*
This means you are trying to assign two different types.
Instead, do this:
cell end = cell(1,1,1,true);
maze[15][15] = end;
or just
maze[15][15] = cell(1,1,1,true);
Since you are using C++, consider looking into std::array
instead. And avoid new/delete when possible.
'maze' does not name a type
In fact, 'maze' DOES NOT name a type. It is, indeed, an object.
In other languages you can write instructions outside the functions, since the whole file body is considered a "function". In C, however, outside functions you can only write declarations and definitions of global variables. You should have written:
#include "Arduino.h"
#include "cell.h"
#include <cell.h>
cell maze[16][16];
cell * current = new cell(1, 1, 0, false, 0);
cell * end_pt = new cell(1,1,1,true);
void setup()
{
maze[15][15] = end_pt;
}
Now, as the other answer pointed out, you can't assign a pointer to the value. If you want to keep maze as a cell matrix, you have to copy the values hand by hand:
void copyCell(cell *dst, cell src)
{
dst->right = src.right;
dst->left = src.left;
dst->forw = src.forw;
dst->info = src.info;
dst->value = src.value;
}
void setup()
{
copyCell(&(maze[15][15]), end_pt);
}
(or better just include a copy function in the class)
OR declare maze as a cell pointers matrix:
cell *maze[16][16];
This depends on how you want to implement the program
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.