0

Old C++ code dont compile

I found an old game programming book in my cabinet and thers a code for a simple 2d game loop https://code.sololearn.com/cOmz89EalBUy/#cpp the .exe file on the cd is working, but i cant compile the code in visual studio the function draw_string dont accepts strings maybe someone could try to compile this and tell me how to fix this in the new vs version

31st Aug 2019, 11:48 AM
Maxim Schiffmann
Maxim Schiffmann - avatar
4 Réponses
+ 1
The dots are moving on my screen and clear screen is clearing the screen. Not sure what else it could be. The only things I changed to make the code compile on visual studio was change the Draw_String 3rd parameter to a const char*, void main into int main and kbhit into _kbhit.
31st Aug 2019, 2:44 PM
Dennis
Dennis - avatar
+ 2
A string literal like "abc" is a const char[N] which converts to a const char* the first chance it gets and it is placed in the read only parts of memory. char* s = "abc" is therefore illegal C++. It would imply that s is pointing to a modifiable string which it is not, even though some compilers allow this anyway but it's gonna crash if you try to write. ( -Wwrite-strings ) will emit a warning. Visual studio does not allow this conversion to happen though. Your Draw_String is defined as inline void Draw_String( int x, int y, char *string ); But it recieves a const char* instead and this conversion to a char* is not allowed. The correct definition should be inline void Draw_String( int x, int y, const char *string ); kbhit might also complain. _kbhit fixes it for me. Check out https://docs.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-getkeyboardstate instead as a possible alternative.
31st Aug 2019, 2:06 PM
Dennis
Dennis - avatar
+ 1
works with const char, but not like the .exe on the disk the dots are not moving and the clear_screen dont work
31st Aug 2019, 2:28 PM
Maxim Schiffmann
Maxim Schiffmann - avatar
+ 1
Error found! I made a new empty Project in VS and now its working fine
31st Aug 2019, 4:13 PM
Maxim Schiffmann
Maxim Schiffmann - avatar