Arduino:for/while/if在void setup()或void loop()之前?——错误:之前需要不合格
Arduino: for/while/if before void setup() or void loop()? -- error: expected unqualified-id before
为什么这不起作用?
for(int i=0; i<5;i++){
// Do stuff
}
void setup() {
// put your setup code here, to run once:
}
void loop() {
// put your main code here, to run repeatedly:
}
在我的具体情况下,我想用参数化构造函数初始化对象数组,如下所示:https://www.geeksforgeeks.org/how-to-initialize-array-of-objects-with-parameterized-constructors-in-c/
我剪下的代码是:
MoistSensor* moistsensors = (MoistSensor*)malloc(sizeof(MoistSensor) * cfg.NumberOfMoistsensors);
for(int it=0; it < cfg.NumberOfMoistsensors; it++) {
moistsensors[it] = MoistSensor(cfg.sensorPins[it], cfg.OnPWM, cfg.OffPWM, cfg.analogInPin);
}
MoistsensorClass是我想要生成的对象,cfg是保存配置数据的对象。
您想要将初始化代码移动到setup
函数中,如下所示:
MoistSensor* moistsensors = nullptr;
void setup() {
moistsensors = (MoistSensor*)malloc(sizeof(MoistSensor) * cfg.NumberOfMoistsensors);
for(int it=0; it < cfg.NumberOfMoistsensors; it++) {
moistsensors[it] = MoistSensor(cfg.sensorPins[it], cfg.OnPWM, cfg.OffPWM, cfg.analogInPin);
}
}
void loop() {
// put your main code here, to run repeatedly:
}
main()
函数。
任何函数之外的东西只能是declarations
、functions
、preprocessor directives
等;但不是可执行代码。
在Arduino IDE中,执行的代码由两个函数编写:
void setup()
和void loop()
void setup()
中的代码在程序执行期间只执行一次。
void loop()
中的代码等效于while(true)
循环中写入的代码
因此,除了initialization of variables
、function definitions
、function declarations
和其他一些东西之外,所有其他类似for
循环的东西都必须写入其中一个函数中
相关文章:
- Arduino:for/while/if在void setup()或void loop()之前?——错误:之前需要不合格
- 为什么我在使用void函数时得到错误代码C2276
- 错误:在为指针赋值时,void值没有被忽略
- 如何在C++中使用 Void 而不会收到有关参数的错误
- QNX Momentics: GoogleMock - 返回有价值,在函数中返回 void 错误
- C++ 随机数生成器:尝试将结果作为向量获取,但通过制作 void 函数来执行此操作而出现错误
- 为什么在将 void 指针转换为整数指针时出现分段错误
- C++命名空间中调用 void 函数的错误
- 错误:"cast"未命名类型void setCastDescription(std::string
- 错误:对类型 'const ItemInstance' 的引用无法绑定到类型 'void' 的右值
- 错误LNK2019未解析的外部符号"public: __thiscall SLinkList<char>::SLinkList<char>(void)"
- 错误:从'void*'到'const uint8_t* {aka const unsigned char*}'的转换无效 [-允许]
- 查找素数:错误:控件到达非void函数的末尾
- 函数 LNK2019 "int __cdecl __scrt_common_main_seh(void)" 中引用的未解析外部符号主错误 (?__scrt_common_main_seh@@YAHXZ
- C++错误(从不兼容的类型"void"分配给树节点*)
- 错误:为什么"void*"不是指向对象的指针类型,即使指针设置为对象?
- 为什么删除void*是UB而不是编译错误?
- 错误 C2084:函数"int main(void)"已具有主体
- 尝试创建 posix 线程并获得从 'void*' 到 'void* (__attribute__((__cdecl__)) *)(void*) 错误的无效转换
- 覆盖 'virtual void ' C++ 错误