C++ struct and function declaration. Why doesn#39;t it compile?(C++结构和函数声明。为什么它不能编译?)
本文介绍了C++结构和函数声明。为什么它不能编译?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
这编译得很好(Arduino):
struct ProgressStore {
unsigned long ProgressStart;
unsigned long LastStored;
uint32_t FirstSectorNr;
};
void IRAM_ATTR ProgressInit(ProgressStore aProgressStore){
}
省略IRAM_ATTR,它将不再编译(?):
Verbruiksmeter:116:6: error: variable or field 'ProgressInit' declared void
116 | void ProgressInit(ProgressStore aProgressStore){//, uint32_t SectorNr) {
| ^~~~~~~~~~~~
Verbruiksmeter:116:19: error: 'ProgressStore' was not declared in this scope
116 | void ProgressInit(ProgressStore aProgressStore){//, uint32_t SectorNr) {
| ^~~~~~~~~~~~~
推荐答案
查看此处:https://stackoverflow.com/a/17493585/2027196
Arduino做到了这一点,它在Main中找到您的所有函数定义,并为代码的睡觉上方的每个函数生成一个函数声明。结果是您试图在声明ProgressStore结构之前使用ProgressStore。我认为IRAM_Attr必须抑制此行为。
它最终在编译前生成以下代码:
void ProgressInit(ProgressStore aProgressStore); // <-- ProgressStore not yet declared
struct ProgressStore {
unsigned long ProgressStart; //Value of the counter at the start of the sector
unsigned long LastStored; //Should be new CounterValue-1, but you never know...
uint32_t FirstSectorNr; //1st of 2 sectors used for storage of progress
};
void ProgressInit(ProgressStore aProgressStore) {//, uint32_t SectorNr) {
// ProgressStore.1stSector = SectorNr;
}
一种解决方案是将您的结构和类移动到它们自己的.h
文件中,并将它们包括在顶部。
ProgressStore.h
#ifndef PROGRESS_STORE_H
#define PROGRESS_STORE_H
struct ProgressStore {
unsigned long ProgressStart; //Value of the counter at the start of the sector
unsigned long LastStored; //Should be new CounterValue-1, but you never know...
uint32_t FirstSectorNr; //1st of 2 sectors used for storage of progress
};
#endif // PROGRESS_STORE_H
main.cpp
#include "ProgressStore.h"
void ProgressInit(ProgressStore aProgressStore) {//, uint32_t SectorNr) {
// ProgressStore.1stSector = SectorNr;
}
函数声明仍是自动生成的,但会插入#include
%s
这篇关于C++结构和函数声明。为什么它不能编译?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
沃梦达教程
本文标题为:C++结构和函数声明。为什么它不能编译?


猜你喜欢
- OpenGL 对象的 RAII 包装器 2021-01-01
- XML Schema 到 C++ 类 2022-01-01
- 将 hdc 内容复制到位图 2022-09-04
- 将函数的返回值分配给引用 C++? 2022-01-01
- 哪个更快:if (bool) 或 if(int)? 2022-01-01
- 从父 CMakeLists.txt 覆盖 CMake 中的默认选项(...)值 2021-01-01
- DoEvents 等效于 C++? 2021-01-01
- GDB 不显示函数名 2022-01-01
- 使用 __stdcall & 调用 DLLVS2013 中的 GetProcAddress() 2021-01-01
- 如何提取 __VA_ARGS__? 2022-01-01