我正在开发一个用 C++ 编写的 Qt5 项目。构建项目报错:
C2440: '=': cannot convert from 'const char [9]' to 'char*'
它指向下面的代码行:
port_name= "\\\\.\\COM4";//COM4-macine, COM4-11 Office
SerialPort arduino(port_name);
if (arduino.isConnected())
qDebug()<< "ardunio connection established" << endl;
else
qDebug()<< "ERROR in ardunio connection, check port name";
//the following codes are omitted ....
这里有什么问题,我该如何解决?
最佳答案
在 C++ 中,与 C 不同,字符串文字是 const
。因此,指向此类字符串文字的任何指针也必须是 const
:
const char* port_name = "\\\\.\\COM4"; // OK
// char* port_name = "\\\\.\\COM4"; // Not OK
关于c++ - C2440 : '=' : cannot convert from 'const char [9]' to 'char*' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54100488/