c++ - printf 的奇怪情况

标签 c++ embedded

我用 st nucleo 和 Shiled GSM (SIM900) 编写了这个简单的程序。我想将答案保存在字符串变量中。 我将数据保存在变量中。一切正常,但是当我使用 printf 命令时, 我有这个错误:

cannot pass object of non-pod type 'string' (aka 'basic_string<char,char_traits<char>,allocator<char>') through variadic method; call will abort at runtime (Wnon-pod-varargs)
#include "mbed.h"
#include <string>
Serial pc(SERIAL_TX, SERIAL_RX); // PC comunication
Serial SIM900(PA_9, PA_10);    // serial comunication
 DigitalOut myled(LED1);
 DigitalOut sim_power(D9);  //power gsm900
string result;
char x;
int i;
void callback_rx() {
while (SIM900.readable()) {
 x = SIM900.getc();
 result += x;
 pc.putc(x);  }
 pc.printf("%s",result);  //her i have error
 }
 void controlAT(){ 
 result = "";
 SIM900.printf("AT\r");
 wait_ms(1000);  }

 int main()
 {

power();
 pc.printf("\r\n GSM 900 TEST\n"); 
 SIM900.attach(&callback_rx);  //call interrupt
 SIM900.baud(9600);
while(1) {
controlAT();
wait_ms(300);
}
}

不知道哪里出了问题;你能帮帮我吗?

最佳答案

使用:

 pc.printf("%s", result.c_str()); 

s 转换规范需要一个 C 字符串参数(即,一个以 null 结尾的 char 数组)。

关于c++ - printf 的奇怪情况,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26237517/

相关文章:

c++ - “数组”不是 'std' 的成员

c++ - PostgreSQL C++ libpq编码UTF-8问题

c - 高效划分大功能

c++ - 嵌入式系统中的音频检测

c - 十六进制的 unsigned long long 的值是多少

c++ - 如何正确格式化 DEVPROP_TYPE_DATE?

c++ - C++ 中的 vector 与数组?

C++:打印 map 值

c - Microchip XC32 编译器,malloc 调用返回 0,写入指针抛出未实现的 RAM

c++ - 嵌入式系统的独立于硬件的C++ HAL