![](/img/trans.png)
[英]No suitable conversion function from “std::string” to “const char *” exists
[英]How to fix “no suitable conversion function from ”String“ to ”const char *“ exists”?
我不确定如何实现这一点,我已经搜索并尝试了许多不同的东西,但我不能让它工作? strcpy(host,DATA); 是什么给了我错误。
char host[60] = "www.yahoo.com";
void loop() {
String content = "";
char character;
while (Serial.available()) {
character = Serial.read();
content.concat(character);
}
if (content != "") {
String CMD = getValue(content, '|', 0);
String DATA = getValue(content, '|', 1);
if (CMD == "SSID")
{
Serial.println("Your CMD is:" + CMD + " And your DATA is:" + DATA);
}
else if (CMD == "PASS")
{
Serial.println("Your CMD is:" + CMD + " And your DATA is:" + DATA);
}
else if (CMD == "HOST")
{
Serial.println("Your CMD is:" + CMD + " And your DATA is:" + DATA);
strcpy(host, DATA);
}
}
delay(100);
}
如果String
是一个std::string
,你可以这样做
strcpy(host, DATA.c_str());
如果它是一个自定义类,例如
class String
{
char buffer[20];
//add a method c_str() for example like the std::string does
const char * c_str() { return buffer; }
//or just go yolo and overload the operator const char * like a boss
operator const char *() { return buffer; }
};
现在你要做的就是strcpy(buff, DATA)
并且将使用operator const char * ()
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.