简体   繁体   English

Library.cpp中的Access数组

[英]Access Array in Library.cpp

this is probably a beginners question (I did not go to school for this), but it is stalling me for two days now. 这可能是一个初学者的问题(我没有为此而去学校),但现在它让我拖延了两天。

I am writing a sketch in Arduino to move a robot. 我正在Arduino写一个草图来移动一个机器人。 I store my positions in an array like 我将我的位置存储在一个数组中

int joint1[180];

I got it perfectly working and running if everything is in the robot.ino, but now I want to put this array in a home-made library stringer.h and stringer.cpp but acces it from the .ino to make it a little easier to understand for someone else. 我得到了它完美的工作和运行,如果一切都在robot.ino,但现在我想把这个阵列中的一个自制库stringer.hstringer.cpp但从.ino存取权限它,使之变得更轻松去了解别人。

the question redefined i readout a home made jcode from sd in the form of a string i put the values to a int whit the toint comand then i store this value in 7 arrays joint1[180] joint2[180] etc now i want to use the array in my main script robot.ino how do i acces the array whitin stringer.ccp or do i put the arrays in my .ino file and make stringer send a string to robot.ino and devide it there ..... this makes it realy hard for the other functions in stringer ???? 重新定义了这个问题,我以字符串形式从sd中读取了一个自制的jcode,然后将这些值放入int里面,然后将其存储在7个数组中。joint1 [180] joint2 [180]等我的主脚本robot.ino中的数组如何添加whitin stringer.ccp数组,或者将数组放入.ino文件中并让Stringer将字符串发送到robot.ino并在此处指定它.....使得stringer中的其他功能真的很难吗????

test situation globals.h 测试情况globals.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];

globals.cpp globals.cpp

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

tester.ino tester.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;}
}

the other way 另一种方法

globals.h globals.h

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

globals.cpp globals.cpp

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

i wil get a error: #include nested too deeply 我会得到一个错误:#include嵌套太深

Don't use arrays. 不要使用数组。 They're bad, especially since you're new to programming. 它们很糟糕,特别是因为你是编程新手。 Use an std::vector instead. 使用std::vector代替。 Declare the vector in a header file, like globals.h : 在头文件中声明向量,如globals.h

#ifndef GLOBALS_H
#define GLOBALS_H
#include <vector>

extern std::vector<int> joint1;

#endif

Note the extern keyword. 注意extern关键字。 This means that this is only a declaration for joint1 . 这意味着这仅是joint1的声明。 Define the vector in a source file, like globals.cpp : 在源文件中定义向量,如globals.cpp

#include "globals.h"

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

When you compile, make sure to also compile globals.cpp with the rest of your sources. 编译时,请确保同时使用其余源代码编译globals.cpp

In source files where you need access to joint1 , make sure to: 在您需要访问joint1源文件中,请确保:

#include "globals.h"

You can access elements in the vector the same way as you do with arrays: 您可以像使用数组一样访问向量中的元素:

joint1[index]

Although since you said you're new to programming, I'd recommend you use the at() function of vector instead: 虽然既然你说你是编程新手,我建议你使用vector的at()函数代替:

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

I recommend it because it checks whether you're trying to access an element outside the vector. 我推荐它,因为它会检查你是否正在尝试访问向量之外的元素。 If for example you try: 例如,如果您尝试:

joint.at(200)

the program with abort with an exception. 具有异常中止的程序。

Now, with that being said, having global variables like this gets tedious and difficult to work with. 话虽如此,拥有这样的全局变量变得乏味且难以使用。 It's better to not use global variables and instead define your functions to take the data you need in function arguments. 最好不要使用全局变量,而是定义函数以在函数参数中获取所需的数据。

If you don't have access to the C++ standard library, and thus std::vector isn't available, then you can keep using arrays instead. 如果您无法访问C ++标准库,因此std :: vector不可用,那么您可以继续使用数组。 extern declaration in globals.h : globals.h extern声明:

extern int joint1[180];

and definition in the .cpp: 和.cpp中的定义:

int joint1[180];

No more .at() or anything else though. 没有更多的.at()或其他任何东西。 Just plain old [index] . 只是普通的[index]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM