[Error record] C++ string constant parameter error (unable to convert parameter 1 from "const char [4]" to "char *" | Conversion from string literal will lose the const qualifier)





1. Error message



Defines a function that receives string parameters of type char*;

// 接收字符串参数并打印
void fun(char* str) {
    
    
	cout << str << endl;
}

If you pass in a string constant, such as "Hello",

	// 传入常量字符串
	fun("Hello");

The complete code is as follows:

#include "iostream"
using namespace std;

// 接收字符串参数并打印
void fun(char* str) {
    
    
	cout << str << endl;
}

int main() {
    
    

	// 传入常量字符串
	fun("Hello");


	// 控制台暂停 , 按任意键继续向后执行
	system("pause");
	return 0;
};

Error message: This error is a compilation error;

Test.cpp(12,13): error C2664: "void fun(char *)": cannot convert parameter 1 from "const char [6]" to "char *"
Test.cpp(12,6): message: Conversion from string literal will lose const qualifier (see /Zc:strictStrings)
Test.cpp(5,6): message : see declaration of "fun"

Complete error report:

已启动生成…
1>------ 已启动生成: 项目: HelloWorld, 配置: Debug Win32 ------
1>Test.cpp
1>D:\002_Project\006_Visual_Studio\HelloWorld\HelloWorld\Test.cpp(12,13): error C2664:void fun(char *): 无法将参数 1 从“const char [6]”转换为“char *1>D:\002_Project\006_Visual_Studio\HelloWorld\HelloWorld\Test.cpp(12,6): message : 从字符串文本转换将丢失 const 限定符(请参阅 /Zc:strictStrings)
1>D:\002_Project\006_Visual_Studio\HelloWorld\HelloWorld\Test.cpp(5,6): message : 参见“fun”的声明
1>已完成生成项目“HelloWorld.vcxproj”的操作 - 失败。
========== 生成: 成功 0 个,失败 1 个,最新 0 个,跳过 0==========

Insert image description here





2. Problem analysis



This error only occurs in higher versions of Visual Studio, such as Visual Studio 2017, Visual Studio 2019 or higher;

In Visual Studio 2013, no error will be reported;


In the following fun function, a character array/string of type char* is received,

// 接收字符串参数并打印
void fun(char* str) {
    
    
	cout << str << endl;
}

If the "Hello" parameter is passed in when calling, this is of const char* type, and the two parameter types do not match;


Ideas to solve the above problems:

  • Modify function parameter type;
  • Modify actual parameter type;
  • Set the compatible configuration of the Visual Studio compilation environment;




3. Solution



1. Set the compatibility rules of Visual Studio


Set Visual Studio compatibility rules:

Right-click the solution in the Solution Explorer and select the last property option in the pop-up menu.

Insert image description here
After opening, go to the configuration properties/C/C++/Language panel and check that the current compliance mode configuration is "Yes (/permissive-)",

Insert image description here

Modify the configuration matching the pattern to "No",

Insert image description here

At this point the program can be executed normally:

Insert image description here


2. Modify the actual parameter type ①


If the function receives a string of type char*, then pass in the actual parameter of type char*, do not pass in a string of type const char*;

Convert the "Hello" string constant to the char* type, as shown in the following code example:

fun((char*)"Hello");

The complete code is:

#include "iostream"
using namespace std;

// 接收字符串参数并打印
void fun(char* str) {
    
    
	cout << str << endl;
}

int main() {
    
    

	fun((char*)"Hello");


	// 控制台暂停 , 按任意键继续向后执行
	system("pause");
	return 0;
};

execution succeed :
Insert image description here


3. Modify the actual parameter type ②


Put the string into a char array and pass the char array as an argument to the function;

	char str[8] = "Hello";
	fun(str);

Complete code example:

#include "iostream"
using namespace std;

// 接收字符串参数并打印
void fun(char* str) {
    
    
	cout << str << endl;
}

int main() {
    
    

	char str[8] = "Hello";

	fun(str);


	// 控制台暂停 , 按任意键继续向后执行
	system("pause");
	return 0;
};

execution succeed :

Insert image description here


4. Modify the actual parameter type ③


Previously, forced type conversion in C language was used. Here, forced type conversion in C++ is used to convert constants into non-constants, and the const_cast operator is used for conversion.

fun(const_cast<char*>("Hello"));

Complete code example:

#include "iostream"
using namespace std;

// 接收字符串参数并打印
void fun(char* str) {
    
    
	cout << str << endl;
}

int main() {
    
    

	fun(const_cast<char*>("Hello"));


	// 控制台暂停 , 按任意键继续向后执行
	system("pause");
	return 0;
};

execution succeed :

Insert image description here


5. Modify formal parameter type


This problem can also be solved by changing the formal parameter of type char* in the function to type const char*;

Complete code example:

#include "iostream"
using namespace std;

// 接收字符串参数并打印
void fun(const char* str) {
    
    
	cout << str << endl;
}

int main() {
    
    

	fun("Hello");


	// 控制台暂停 , 按任意键继续向后执行
	system("pause");
	return 0;
};

Results of the :

Insert image description here

Guess you like

Origin blog.csdn.net/han1202012/article/details/135358853