[英]strange situation with printf
我用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
数组)。
使用%s规范时,必须在printf中使用C样式字符串。 如果使用printf,则必须使用c_str()将其转换为C样式字符串。 以下解决方案将适合您的情况
1. pc.printf("%s", result.c_str());
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.