c++ - C2440 : '=' : cannot convert from 'const char [9]' to 'char*'

标签 c++ arduino qt5

我正在开发一个用 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/

相关文章:

c++ - C++ 中的纯/常量函数

c++ - 为什么 operator""s 隐藏在命名空间中?

c++ - 递归有限制吗?

c++ - 在 avr g++ 中禁用函数声明错误

c++ - 编译 Qt 5.0 时出错

c++ - 如何将 emscripten 与 cmake 一起用于项目依赖项?

c++ - Arduino IDE中自定义库类的.h文件编译错误的原因是什么

arduino - AVR USART通讯问题

qt - 以编程方式查找最新 Qt 版本号的最简单方法是什么?

linux - GStreamer;无法暂停 - "";错误: "No URI set" - MediaPlayer QML Type