How do you fix errors E0167 and C2664 when drawing lines with graphics.h in Visual Code 2019?

851 Views Asked by At

When I try to draw lines in Visual Studio 2019 with graphics.h I keep getting 2 types of errors.

This is an example I'm trying to get to work on my compiler from geeksforgeeks.org

Errors

E0167 - argument of type "const char*" is incompatible with lies 2 parameter of type "char*" - Line 17

and

C2664 - 'void initgraph(int*,int*,char*)'; cannot convert argument 3 from 'const char[1] to 'char*' - Line 17

My code:

'''// C++ Implementation for drawing line 
#include <graphics.h> 

// driver code 
int main()
{
    // gm is Graphics mode which is a computer display 
    // mode that generates image using pixels. 
    // DETECT is a macro defined in "graphics.h" header file 
    int gd = DETECT, gm;

    // initgraph initializes the graphics system 
    // by loading a graphics driver from disk 
    initgraph(&gd, &gm, "");

    // line for x1, y1, x2, y2 
    line(150, 150, 450, 150);

    // line for x1, y1, x2, y2 
    line(150, 200, 450, 200);

    // line for x1, y1, x2, y2 
    line(150, 250, 450, 250);

    getch();

     // closegraph function closes the graphics 
    // mode and deallocates all memory allocated 
    // by graphics system . 
    closegraph();
}'''
1

There are 1 best solutions below

0
Hajo Kirchhoff On

Use /Zc:strictStrings- as a compiler switch (it's also in the Visual Studio project settings. See here https://learn.microsoft.com/en-us/cpp/build/reference/zc-strictstrings-disable-string-literal-type-conversion?view=msvc-170&viewFallbackFrom=vs-2019

and here https://www.codetd.com/en/article/12953118

for detailed information.

In older compilers, a string literal such as "" was a char*, which can be passed to initgraph. But with more modern C++ standards, string literals are now const char*, which cannot be passed to a char* parameter.