由于不推荐使用从字符串常量到 'char*' [-Wwrite-strings] 的转换而出错

getting error as deprecated conversion from string constant to 'char*' [-Wwrite-strings]

我收到错误消息,因为 不赞成在 initgraph() 函数

中从字符串常量到 'char*' [-Wwrite-strings] 的转换
// C Implementation for drawing circle 
#include <graphics.h> 

//driver code 
int main() 
{ 
    int gd = DETECT, gm; 

    initgraph(&gd, &gm, ""); //here I'm getting an error of string

    circle(250, 200, 50); 

    getch(); 

    closegraph(); 

    return 0; 
} 

"" 是一个 c 风格的 string literal,其类型是 const char[1] 并且可以隐式转换为 const char*。自 C++11 以来,不允许隐式转换为 char*

In C, string literals are of type char[], and can be assigned directly to a (non-const) char*. C++03 allowed it as well (but deprecated it, as literals are const in C++). C++11 no longer allows such assignments without a cast.

initgraphconst char*最好。否则你必须使用 const_cast 执行显式转换;但请注意,尝试修改字符串文字会导致未定义的行为。如果 initgraph 可以,那么您不应该传递字符串文字。