【问题标题】:Arduino - Creating an Array from instances and assigning value [C++]Arduino - 从实例创建数组并赋值 [C++]
【发布时间】:2016-05-30 16:51:26
【问题描述】:

我搜索了很多关于 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' 没有命名类型是我的错误。请帮助并提前感谢您!

【问题讨论】:

    标签: c++ arrays class multidimensional-array arduino


    【解决方案1】:

    这行有问题:
    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。并尽可能避免新建/删除。

    【讨论】:

    • 当我删除星号时,我遇到了非标量/标量问题。这是什么意思?
    • @MertKarakas 我认为您的代码仍然看起来像:cell end_pt = new cell(1,1,1,true); ?同时删除 new 关键字。 new 创建一个指针。
    • @DeathTails 你确定你可以像这样复制两个对象吗?我认为您应该重载 = 运算符才能以这种方式复制它们
    • @frarugi87 只要对象不拥有任何资源,编译器创建的默认赋值运算符就足够了。
    • @DeathTails 不知道,但我只是测试了它,并且......它有效!谢谢
    【解决方案2】:

    'maze' 没有命名类型

    事实上,“迷宫”并没有命名类型。它确实是一个对象。

    在其他语言中,您可以在函数之外编写指令,因为整个文件体都被视为“函数”。然而,在 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];
    

    这取决于你想如何实现程序

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-01-02
      • 2016-05-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-08-04
      • 1970-01-01
      • 2023-01-03
      相关资源
      最近更新 更多