Arduino/C :从库中访问结构的特定标头文件
Arduino/C++: Access specific header file of structs from library
(不确定这是否仅是C/C 问题)
我目前正在将一个大型Arduino项目的元素分解为可重复使用的库 - 到目前为止,SOO很好。
但是,库中的许多方法返回特殊结构,这些结构在每个库中包含的data-types.h
文件中声明。我现在遇到的问题是我无法在主要草图中导入/利用这些结构。我已经尝试在主库标头文件中声明DataTypes
类的变量并通过它访问structs,但是我会收到错误error: invalid use of 'struct DataTypes::_theStructNameHere_t'
我将如何从主要草图中的库中访问这些结构以将其声明为变量类型?我不想复制包含库中构造的标题文件中的标题文件,我也不想为structs的单个标头文件创建一个单独的库!
!这是我的意思的快速示例:
main.cpp:
#include <Arduino.h>
#include <MyLibrary.h>
MyLibrary myLib;
void setup() {
(This is declared in the library) myLib.dataTypes._theStructNameHere_t response = myLib.getASpecialValueWhichIsOfType_theStructNameHere_t()// Gives "error: invalid use of 'struct DataTypes::_theStructNameHere_t'""
// Example usage of the struct:
Serial.print("n Loop Card Status: ");Serial.print(response.loop_status, HEX);
if (response.number_allocated > 0) {
Serial.print("n Devices Allocated: ");Serial.print(response.number_allocated, HEX);
} else {
if (response.loop_status != 0x123) {
// Some condition
} else {
// Something else
}
}
}
void loop() {
...
}
库结构:
src/
- /data-types/
- - data-types.h
- MyLibrary.cpp
- MyLibrary.h
库标题MyLibrary.h
:
#ifndef _MYLIBRARY_H_
#define _MYLIBRARY_H_
#include <Arduino.h>
#include "./helpers/helpers.h"
...
#include "./data-types/data-types.h"
class MyLibrary {
public:
Uart *_commPort;
Helpers helpers;
...
DataTypes dataTypes;
DataTypes::_theStructNameHere_t getASpecialValueWhichIsOfType_theStructNameHere_t();
...
protected:
private:
};
#endif // _MYLIBRARY_H_
数据类型类data-types.h
:
#ifndef _RESPONSE_TYPES_H
#define _RESPONSE_TYPES_H
class DataTypes
{
public:
struct _theStructNameHere_t
{
bool successful;
uint8_t loop_status;
uint8_t number_allocated;
uint8_t highest_address;
uint8_t number_inputs;
uint8_t number_outputs;
}
..even more..
private:
}
#endif // _RESPONSE_TYPES_H
我能够从您的示例中获得MCVE:
class DataTypes
{
public:
struct _theStructNameHere_t
{
};
};
class Library
{
public:
DataTypes dataTypes;
DataTypes::_theStructNameHere_t getMyDataType();
};
int main(int argc, char *argv[])
{
Library myLib;
myLib.dataTypes._theStructNameHere_t response;
}
给出与您的代码类似的错误:
~$ g++ test.cpp
test.cpp: In function 'int main(int, char**)':
test.cpp:20:21: error: invalid use of 'struct DataTypes::_theStructNameHere_t'
myLib.dataTypes._theStructNameHere_t response;
问题是您使用一个实例访问struct
类型/名称。要修复它,请更换
myLib.dataTypes._theStructNameHere_t response = ...;
DataTypes::_theStructNameHere_t response = ...;
注意:
- 而不是直接使用 namespaces 来考虑使用类创建单独的名称空间。这是
C++
的功能,可在 arduino 中获得。
namespace Library {
namespace DataTypes {
struct _theStructNameHere_t
{
...
};
...
} /*** namespace Library::DataTypes ***/
} /*** namespace Library ***/
请阅读 stackoverflow 有关如何提出一个好问题的准则,特别是有关Mininimal,完整和可验证的示例的部分。
迟早有人会告诉您
C/C++
没有这样的东西;C
是C
,C++
是C++
;Arduino
生活在自己的世界中,即使基于C++
。因此,您可能需要从问题中删除C
和C++
标签。
相关文章:
- C++将文本文件中的数据读取到结构数组中
- 调用 C 文件中的"C++"结构
- 结构包含在两个头文件中,这两个文件我都不拥有
- 在头文件和 cpp 文件中使用一次 #pragma 时出现结构重定义错误
- 介子复制/安装头文件到输出目录并保持文件夹结构
- Windows 将标准的 C 文件结构注入到正在运行的进程中
- 如何读取/查询文件系统和文件结构
- C++/Python 中对象和结构的二进制文件结构
- 在 C++ 和 C# 之间共享内存映射文件结构
- 在文件结构中使用字符串而不是带有 read() 和 write() 的字符数组
- C++ while 循环在自定义文件结构中
- QT C 读取自定义文件结构
- CPP 头文件结构错误
- 安卓rom文件结构和调试
- 文件映射和文件结构
- 文件结构,以避免数据损坏
- 文件处理中给出奇怪值的流(指向文件结构的指针)
- 程序c++程序的文件结构
- 常规c++程序头文件结构
- FreeBSD 和 Linux 中的 ELF 文件结构