0

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();
}'''
genpfault
  • 51,148
  • 11
  • 85
  • 139
  • What is graphics.h? – Thomas Sablik Oct 19 '19 at 20:56
  • 1
    @ThomasSablik graphics.h is a header that used to ship with Turbo C++ some 20 something years ago. It implemented graphics drawing routines for VGA displays (and EGA and CGA) for DOS based computers. It has been obsolete for decades and noone uses it today. It is not standard C++. – Jesper Juhl Oct 19 '19 at 21:13
  • Your MSVC++ compiler is not casting between `const char*` and `char*` automatically which is a nuisance (GCC is the same) the problem is `""` in the `initgraph` you have to make some kind of `cast<>` or use variable instead like `char txt[]=""; initgraph(&gd, &gm, txt);` – Spektre Oct 20 '19 at 07:00
  • @drescherjm I do not think (s)he uses old Turbo C++ compiler, tags are hinting MSVC++ and also in Borland style compilers (old and new) would this casting compile with no problem... Also IIRC older GCC and MSVC++ compilers accepted such casting in the past they changed it at some point... – Spektre Oct 21 '19 at 06:56
  • It should have been updated for `c++11` support. So he/she will need to change the code or use a compiler that has the ability to turn off `c++11`. – drescherjm Oct 21 '19 at 12:24

1 Answers1

0

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.

Hajo Kirchhoff
  • 1,969
  • 8
  • 17