警告:当我在函数def中添加形式参数但没有实际参数

时间:2016-08-17 13:09:31

标签: c++

编译时,下面的代码不会抛出任何警告

 #include <iostream>

 void GetHMACCode(unsigned char* buffer,long bufferLength,unsigned char** pResult,unsigned int &nResultLen){}

 unsigned char *pCRCBufferptr ;

 main() {
 unsigned char *pHMAC  = NULL;
 int ncrcDataLength;
 unsigned int nHMACLen = 0;
 GetHMACCode(pCRCBufferptr,ncrcDataLength,&pHMAC,nHMACLen);

 }

但是当我在函数中添加一个额外的参数(而不是在函数调用中)时,我得到以下警告

myFile.cpp: In function ‘int main()’:
myFile.cpp:11: warning: deprecated conversion from string constant to ‘char*’

在函数

中添加了额外的争论
 #include <iostream>

 void GetHMACCode(unsigned char* buffer,long bufferLength,unsigned char** pResult,unsigned int &nResultLen,char *extra_arg = "11111111111111111111"){}

 unsigned char *pCRCBufferptr ;

 main() {
 unsigned char *pHMAC  = NULL;
 int ncrcDataLength;
 unsigned int nHMACLen = 0;
 GetHMACCode(pCRCBufferptr,ncrcDataLength,&pHMAC,nHMACLen);

 }

我很困惑为什么这个警告会被抛出

由于 特加斯

2 个答案:

答案 0 :(得分:1)

添加const解决了您的问题:

void GetHMACCode(unsigned char* buffer,
                 long bufferLength,
                 unsigned char** pResult,
                 unsigned int &nResultLen,
                 const char *extra_arg = "11111111111111111111") // const added here
{}

答案 1 :(得分:1)

警告是关于将字符串文字转换为非常量字符*(char* extra_arg = "...")。

将您的功能定义更改为以下内容应该会使警告消失:

void GetHMACCode(unsigned char* buffer,long bufferLength,unsigned char** pResult,unsigned int &nResultLen, const char *extra_arg = "11111111111111111111"){}