c++ - 访问 Library.cpp 中的数组

标签 c++ arrays

这可能是一个初学者问题(我没有为此上学),但它现在让我拖延了两天。

我正在用 Arduino 写草图来移动机器人。我将我的位置存储在一个数组中,例如

int joint1[180];

如果一切都在 robot.ino 中,我可以完美地运行它,但现在我想把这个数组放在一个自制的库 stringer.hstringer.cpp 中 但从 .ino 访问它,使其他人更容易理解它。

问题重新定义我以字符串的形式从 sd 中读出一个自制的 jcode 我将值放入一个 int whit 指令然后我将这个值存储在 7 个数组 joint1[180] joint2[180] 等现在我想在我的主脚本 robot.ino 中使用数组 我如何访问数组 whitin stringer.ccp 或者我将数组放在我的 .ino 文件中并让 stringer 发送一个字符串到 robot.ino 并在那里分配它...... .. 这使得 stringer 中的其他功能真的很难 ????

测试情况 全局.h

//#include "Arduino.h"
//#include "StandardCplusplus.h"
//#include <vector>
#include "globals.h"



extern int Joint1[180];
extern int Joint2[180];
extern int Joint3[180];
extern int Joint4[180];
extern int Joint5[180];
extern int Joint6[180];
extern int Slomo[180];

全局变量

//#include "Arduino.h"
//#include "StandardCplusplus.h"
//#include <vector>
#include "globals.h"


int Joint1[180];
int Joint2[180];
int Joint3[180];
int Joint4[180];
int Joint5[180];
int Joint6[180];
int Slomo[180];

测试器.ino

//#include "StandardCplusplus.h"
#include <globals.h>

int check = 100;
int temp = 0; 

void setup() {
for (int p = 0;p < check; p++){
Joint1[p] = p + 33;}
}

void loop() {
if (temp < check){Serial.println(Joint1[temp]);temp = temp + 1;}
}

另一种方式

全局变量

#include "Arduino.h"
#include "StandardCplusplus.h"
#include <vector>
#include "globals.h"



extern std::vector<int> Joint1;
extern std::vector<int> Joint2;
extern std::vector<int> Joint3;
extern std::vector<int> Joint4;
extern std::vector<int> Joint5;
extern std::vector<int> Joint6;
extern std::vector<int> Slomo;

全局变量

#include "Arduino.h"
#include "StandardCplusplus.h"
#include <vector>
#include "globals.h"


std::vector<int> Joint1(180);
std::vector<int> Joint2(180);
std::vector<int> Joint3(180);
std::vector<int> Joint4(180);
std::vector<int> Joint5(180);
std::vector<int> Joint6(180);
std::vector<int> Slomo(180);

我会得到一个错误:#include 嵌套太深

最佳答案

不要使用数组。它们很糟糕,尤其是因为您是编程新手。请改用 std::vector。在头文件中声明 vector ,例如 globals.h:

#ifndef GLOBALS_H
#define GLOBALS_H
#include <vector>

extern std::vector<int> joint1;

#endif

注意 extern 关键字。这意味着这只是 joint1 的声明。在源文件中定义 vector ,例如 globals.cpp:

#include "globals.h"

std::vector<int> joint1(180);

编译时,请确保还使用其余源代码编译 globals.cpp

在需要访问 joint1 的源文件中,确保:

#include "globals.h"

您可以像访问数组一样访问 vector 中的元素:

joint1[index]

虽然你说你是编程新手,但我建议你改用 vector 的 at() 函数:

something = joint1.at(index);
joint1.at(index) = something;
// etc.

我推荐它是因为它会检查您是否正在尝试访问 vector 外部的元素。例如,如果您尝试:

joint.at(200)

异常中止的程序。

话虽如此,拥有这样的全局变量变得乏味且难以使用。最好不要使用全局变量,而是定义函数以获取函数参数中所需的数据。

如果您无权访问 C++ 标准库,因此 std::vector 不可用,那么您可以继续使用数组。 globals.h 中的 extern 声明:

extern int joint1[180];

和.cpp中的定义:

int joint1[180];

没有更多的 .at() 或其他任何东西。只是普通的旧[index]

关于c++ - 访问 Library.cpp 中的数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36235289/

相关文章:

arrays - 对象数组上的Elasticsearch脚本过滤器

c++ - 使用 memset 初始化整个数组

c++ - 填充对象的 C++ 二维数组

c - 在 MIPS 程序集中交换数组中的元素? (比较清楚的)

javascript - 访问数组内对象的值

c++ - 将 listS 用于顶点和边列表时无法调用 boost::clear_vertex

c++ - C++ 构造函数中的 'new auto'

c++ - QJSEngine 删除了我的 QObject,如何在 QJSEngine::newQObject 之后更改所有权?

c++ - gcc 4.5.1 为 C++0x 线程支持配置选项

c++ - QT中鼠标事件是如何传递给widget的