已弃用从字符串常量到 char * 错误的转换

发布于 2024-12-04 15:48:55 字数 748 浏览 1 评论 0原文

可能的重复:
C++ 已弃用从字符串常量到“char*”的转换 < /p>

我有以下代码,尽管我没有复制完整的代码,因为它很大。 以下代码位于模板类中,我收到如下警告。由于模板中的警告,我无法实例化它并收到“从此处实例化”错误。

警告:已弃用从字符串常量到“char*”的转换

void ErrorMessageInRaphsodyCode(char* pcCompleteMessage, char* pcMessage, char* pcFileName, unsigned int RowNo)
{
//...
}


char cCompleteMessage[200];
memset(cCompleteMessage, 0x00, sizeof(cCompleteMessage));
char*cMessage = "add reorgenize failed";
ErrorMessageInRaphsodyCode(cCompleteMessage, cMessage, "omcollec.h", __LINE__);

我的问题是摆脱上述警告的最佳方法是什么?

Possible Duplicate:
C++ deprecated conversion from string constant to ‘char*’

I am having following code, though i didn't copy full code because it is huge.
Following code is in template class, and i am getting warning as below. Because of warning in template i am not able to instantiate it and getting "instantiated from here" error.

warning: deprecated conversion from string constant to 'char*''

void ErrorMessageInRaphsodyCode(char* pcCompleteMessage, char* pcMessage, char* pcFileName, unsigned int RowNo)
{
//...
}


char cCompleteMessage[200];
memset(cCompleteMessage, 0x00, sizeof(cCompleteMessage));
char*cMessage = "add reorgenize failed";
ErrorMessageInRaphsodyCode(cCompleteMessage, cMessage, "omcollec.h", __LINE__);

My question is what is best way to get rid of above warning ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

断念 2024-12-11 15:48:55

如果函数采用 char const *,则它保证只读取指针指向的任何数据。但是,如果它采用非常量指针,例如 char *,它可能会对其进行写入。

由于写入字符串文字是不合法的,因此编译器将发出警告。

最好的解决方案是将函数更改为接受 char const * 而不是 char *

If a function takes a char const *, it guarantees that it only reads whatever data the pointer points to. However, if it takes a non-const pointer, like char *, it might write to it.

As it is not legal to write to a string literal, the compiler will issue a warning.

The best solution is to change the function to accept char const * rather than char *.

爱你是孤单的心事 2024-12-11 15:48:55
char cMessage[] = "add reorganize failed";

这应该消除警告。

char cMessage[] = "add reorganize failed";

This should get rid of the warning.

半世蒼涼 2024-12-11 15:48:55

摆脱它的最佳方法是修复采用参数的函数。

如果您的代码正确并且该函数确实采用字符串常量,则它应该在其原型中如此说明:

void ErrorMessageInRaphsodyCode(char* pcCompleteMessage, char* pcMessage, const char* pcFileName, unsigned int RowNo)

如果你不能这样做(你没有代码),你可以创建一个内联包装器:

inline void ErrorMessageInRaphsodyCodeX(char* p1, char* p2, const char* p3, unsigned int p4)
{  ErrorMessageInRaphsodyCode(p1,p2,(char*)p3,p4); }

并使用包装器代替。

如果您的代码不正确并且该函数实际上需要可写内存(我对此非常怀疑),您将需要通过按照 Jan 的建议创建本地数组或 mallocating 足够的内存来使字符串可写。

Best way to get rid of it is to fix the function that is taking the parameter.

If your code is correct and the function does indeed take string constants, it should say so in its prototype:

void ErrorMessageInRaphsodyCode(char* pcCompleteMessage, char* pcMessage, const char* pcFileName, unsigned int RowNo)

If you can't do that (you don't have the code), you can create an inline wrapper:

inline void ErrorMessageInRaphsodyCodeX(char* p1, char* p2, const char* p3, unsigned int p4)
{  ErrorMessageInRaphsodyCode(p1,p2,(char*)p3,p4); }

and use the wrapper instead.

If your code is incorrect and the function does actually require writeable memory (which I highly doubt), you will need to make the string writeable by either creating a local array as Jan suggested, or mallocating enough memory.

我乃一代侩神 2024-12-11 15:48:55

(1) 将变量设置为 const char*

(..., const char* pcFileName, ...)

(2) 如果上述方法不可行并且您希望保留 的状态char*const char* 然后将该函数设为模板

template<typename CHAR_TYPE>  // <--- accepts 'char*' or 'const char*'
void ErrorMessageInRaphsodyCode(char* pcCompleteMessage, CHAR_TYPE* pcMessage, char* pcFileName, unsigned int RowNo)
{
//...
}

(1) Make the variable a const char*

(..., const char* pcFileName, ...)

(2) If above is not possible and you want to retain the state of char* and const char* then make the function a template:

template<typename CHAR_TYPE>  // <--- accepts 'char*' or 'const char*'
void ErrorMessageInRaphsodyCode(char* pcCompleteMessage, CHAR_TYPE* pcMessage, char* pcFileName, unsigned int RowNo)
{
//...
}
拥抱没勇气 2024-12-11 15:48:55

std::string 类的函数 c_str()

function c_str() of std::string class.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文