簡體   English   中英

Arduino-從實例創建數組並分配值[C ++]

[英]Arduino - Creating an Array from instances and assigning value [C++]

我在C ++和數組上搜索了很多資源。 我了解到數組在c ++中的作用類似於指針,而我對如何創建多維數組以及為索引分配值感到困惑。 我通常使用Java和Python進行編碼,但知道我正在使用Arduino,並且需要學習c ++。

關於此數組的我的Arduino(c ++)代碼為:

#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;

我的.h和.cpp文件;

#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[15][15] = end_pt;

maze[15][15]和迷宮中的任何其他對象都是cell類型
end_pt的類型為cell*

這意味着您正在嘗試分配兩種不同的類型。

相反,請執行以下操作:

cell end = cell(1,1,1,true);
maze[15][15] = end;

要不就

maze[15][15] = cell(1,1,1,true);

由於您使用的是C ++,請考慮改用std::array 並盡可能避免新的/刪除的內容。

“迷宮”沒有命名類型

實際上,“迷宮”不會命名類型。 實際上,它是一個對象。

在其他語言中,您可以在函數外部編寫指令,因為整個文件正文都被視為“函數”。 但是,在C中,外部函數只能編寫全局變量的聲明和定義。 您應該已經寫過:

#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;
}

現在,正如另一個答案指出的那樣,您無法為該值分配指針。 如果要將迷宮作為單元矩陣,則必須手動復制值:

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);
}

(或者最好在類中包含一個復制函數)

或將迷宮聲明為單元格指針矩陣:

cell *maze[16][16];

這取決於您要如何實現該程序

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM