0

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!

2 Answers 2

1

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.

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

5 Comments

When I remove the asteriks, then I have a non-scalar/scalar problem. What does that mean?
@MertKarakas I assume your code still looks like: cell end_pt = new cell(1,1,1,true); ? Remove the new keyword as well. new creates a pointer.
@DeathTails are you sure you can copy two objects like that? I think you should overload the = operator to be able to copy them this way
@frarugi87 As long as the object owns no resources, the default assignment operator created by the compiler will be sufficient.
@DeathTails Didn't know that, but I just tested it and.. It works! Thank you
1

'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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.