c++ - 无法通过 C++ 警告 : (& crash after) deprecated conversion from string constant to ‘char*’ [-Wwrite-strings]

标签 c++ codeblocks dev-c++ bgi

我什么都试过了,连这个简单的图代码都启动不了。我已经在所有正确的文件夹中安装了 graphics.h、winbgim.h 和 libbgi.a。 (包括和库)。链接器也是 -lbgi -lgdi32 -lcomdlg32 -luuid -loleaut32 -lole32

我正在使用 CodeBlocks 16.01。我也在 Dev-C++ 中尝试过,但没有成功。

首先我得到错误:

C:\Program Files (x86)\CodeBlocks\MinGW\include\graphics.h|302|error: redefinition of 'int right'

但是我通过更改graphics.h文件解决了,int right需要替换成int top

还有1个警告-

C:\Users\Davor\Desktop\New Project\drugi\main.cpp||In function 'int main()':| C:\Users\Davor\Desktop\New Project\drugi\main.cpp|15|warning: deprecated conversion from string constant to 'char*' [-Wwrite-strings]| ||=== Build finished: 0 error(s), 1 warning(s) (0 minute(s), 0 second(s)) ===|

在那之后,当我按下“Build and run”时,我得到了

" *.exe has stopped working. " In the background I first get -

Process returned 255 (0xFF) execution time : 91.666 s Press any key to continue.

但每隔一段时间我都会 -

Process returned -1073741819 (0xC0000005) execution time : 26.494 s Press any key to continue.

#include <iostream>
#include <cstdlib>
#include <graphics.h>


using namespace std;



int main() {

    int gdriver = 9;
    int gmode = 2;
    int x;
    initgraph(&gdriver, &gmode, "");
    setbkcolor (WHITE);
    setcolor (BLACK);
    cleardevice();
    for (x = 260; x < 364; x = x + 5)
    {
        circle(x,240,60);
    }
    getch();
    closegraph();

    return 0;
}

最佳答案

不看里面<graphics.h> , 看起来 initgraph()被声明为采用(非常量)char* 作为第三个参数 - 暗示它是一个输出 参数,指向一个可写的 char 数组 initgraph()将写入。

除了生成您引用的警告外,还传递字符串常量 ""因为参数会导致未定义的行为,其中崩溃就是一个很好的例子。

虽然我上面的评论是有效的,但在查看文档后我会推测该参数不是输出参数,并且缺少 const只是糟糕的 API 实现——显然没有任何方法来确定初始化是否真的成功......

关于c++ - 无法通过 C++ 警告 : (& crash after) deprecated conversion from string constant to ‘char*’ [-Wwrite-strings],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37047981/

相关文章:

c++ - 如何在 Windows XP 下从 Dev-c++ 中获取预处理代码?

c - do while 语句只执行两次 code::block 和 dev c++

c++ - 从 C++ 中的动态库访问静态类成员

由 C++ 调用的 Java GUI 程序在关闭时终止其 C++ 控制台应用程序的主线程

c++ - 我怎样才能得到执行我的程序的人的用户名?

c - CodeBlocks C编程错误

c++ - 为什么要定义一个返回结构的 lambda 函数而不是直接定义结构?

c++ - gcc 或 makefile 是否有一种机制可以描述源文件的包含文件以替换 #include "xxxx.h"?

c++ - 从 linux 在 C::B 上交叉编译以赢得 wxwidgets 代码?

c++ - 模板函数只适用于 VS