Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/131.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C2440:'=';:无法从';常量字符[9]';至';字符*'; 我正在研究一个用C++编写的QT5项目。生成项目时出现错误:_C++_Arduino_Qt5 - Fatal编程技术网

C2440:'=';:无法从';常量字符[9]';至';字符*'; 我正在研究一个用C++编写的QT5项目。生成项目时出现错误:

C2440:'=';:无法从';常量字符[9]';至';字符*'; 我正在研究一个用C++编写的QT5项目。生成项目时出现错误:,c++,arduino,qt5,C++,Arduino,Qt5,C2440:“=”:无法从“常量字符[9]”转换为“字符*” 它指向下面的代码行: port_name= "\\\\.\\COM4";//COM4-macine, COM4-11 Office SerialPort arduino(port_name); if (arduino.isConnected()) qDebug()<< "ardunio connection established" << endl; else qDebug()<<

C2440:“=”:无法从“常量字符[9]”转换为“字符*”

它指向下面的代码行:

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 ....
port\u name=“\\\.\\COM4”//COM4 macine,COM4-11办公室
SerialPort arduino(端口名称);
if(arduino.isConnected())

C++中的qDebug Ge(

),与C相反,字符串文字是 const 。因此,指向此类字符串文字的任何指针也必须是

const

const char* port_name = "\\\\.\\COM4";  // OK
// char* port_name = "\\\\.\\COM4";  // Not OK

字符串文字是C++中的常数数据(编译器倾向于将它们存储在只读内存中)。 在C++11及更高版本中,您不能再将字符串文字直接分配给指向非常量字符(

char*
)1的指针

1:虽然一些C++11编译器可能允许它作为向后兼容性的非标准扩展,但可能需要通过编译器标志手动启用

因此,您需要将
port\u name
声明为指向const char的指针(
const char*
char const*
)。但在将其传递到
SerialPort()时,您必须将其转换回非常量
char*


port\u name
声明为
const char*
而不是
char*
。执行此操作后,它会带来与以下代码Arduino(port\u name)相关的另一个错误,无法将参数1从“const char*”转换为“char*”。SerialPort(char*portName)已在别处定义。@jingweimo在将其传递给
SerialPort()
时,必须将其转换回
char*
const char *port_name = "\\\\.\\COM4";
SerialPort arduino(const_cast<char*>(port_name));
SerialPort arduino(const_cast<char*>("\\\\.\\COM4"));
char port_name[] = "\\\\.\\COM4";
SerialPort arduino(port_name);