如何修复"no suitable conversion function from "字符串" to "常量字符* " exists"?

How to fix "no suitable conversion function from "String" to "const char *" exists"?

本文关键字:常量 to 字符 exists 字符串 from no 何修复 suitable conversion function      更新时间:2023-10-16

我不完全确定如何完成此操作,我已经搜索并尝试了许多不同的事情,但我就是无法让它工作? 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 * ()