![](/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.