6

我正在用 C++ 为 Arduino草图编写一个堆栈类。我相信它完全符合 AVR(如果这就是它的名字;我不记得确切的话)编译器;我已经使用了 all mallocandfree而不是newanddelete等等。

所以我的班级有一个 .h 文件。我已将它导入到草图中,但是当我尝试编译时,出现以下错误:

In file included from sketch_may15a.cpp:1:
/CStack.h:58:18: error: string: No such file or directory
In file included from sketch_may15a.cpp:1:
CStack.h:61: error: variable or field 'Error' declared void
CStack.h:61: error: 'string' was not declared in this scope

这是我班的前几行:

#include <string>
using namespace std;
void Error(string message) {

所以Arduino编译器找不到<string>,接下来的几个问题似乎与它有关(不确定是什么variable or field Error declared void意思,我的错误函数只是用于调试目的)。

我知道 Arduino 草图支持字符串而不需要导入,但我不确定它如何与 C/C++/.h 文件一起使用。我试过用谷歌搜索它,但没有太多文档。

4

2 回答 2

8

Arduino 草图不支持任何 C++ 标准库,因为它们是使用不支持它的 avr-libc编译的。但是,Arduino 确实提供了String应该做你需要的类。

如果您正在编写一个库,您还需要#include <Arduino.h>(或者#include <Wiring.h>如果您使用的是 Arduino IDE 的 1.0 之前版本)。

于 2012-05-16T09:47:48.213 回答
1

由于堆碎片和其他问题,建议不要在微处理器中使用 String 类

新的 SafeString 库(可从库管理器中获得)解决了所有这些问题,并且完全安全,永远不会导致您的草图重新启动。

请参阅https://www.forward.com.au/pfod/ArduinoProgramming/SafeString/index.html上的详细教程

于 2020-06-25T00:39:31.967 回答