0

I am desperately trying to make a header file with functions for experimenting with ASCII graphics in console. My goal is to simplify the using of windows.h. I am using Dev-C++.

I wrote a void-type initialization function that sets the window and screen-buffer size, title and cursor size and visibility by using parameters and windows.h functions.

#ifndef _KONGRA_H
#define _KONGRA_H
#include <cstdlib>
#include <iostream>
#include <windows.h>

using namespace std;
HANDLE KGconsole=GetStdHandle(STD_OUTPUT_HANDLE);

//========================================================================CURSOR
bool KGcursorvisible;
int KGcursorsize;
CONSOLE_CURSOR_INFO KGcursorstat;

//========================================================================WINDOW
int KGbuffersizex;
int KGbuffersizey;
COORD KGbuffersize;
int KGwindowsizex;
int KGwindowsizey;
SMALL_RECT KGwindowsize;
TCHAR KGconsoletitle;

void KGset(KGbuffersizex, KGbuffersizey, KGwindowsizex, KGwindowsizey, KGconsoletitle, KGcursorvisible, KGcursorsize)
{
     SetConsoleTitle(KGconsoletitle);
     KGcursorstat.dwSize=KGcursorsize;
     KGcursorstat.bVisible=KGcursorvisible;
     SetConsoleCursorInfo(KGconsole, &KGcursorstat);
     KGbuffersize={KGbuffersizex, KGbuffersizey};
     SetConsoleScreenBufferSize(KGconsole, KGbuffersize);
     KGwindowsize={0, 0, KGwindowsizex-1, KGwindowsizey-1};
     SetConsoleWindowInfo(KGconsole, TRUE, &KGwindowsize);
}

#endif

When whole file included, it keeps giving a compilation error:

24 variable or field `KGset' declared void
25 initializer expression list treated as compound expression
25 `,' or `;' before '{' token 

I can't find a missing character so it's possible that I am wrongly using header.

Maybe it isn't important, but here's main.cpp:

#include <cstdlib>
#include <iostream>
#include <conio.h>
#include "kongra.h"

using namespace std;

int main(int argc, char *argv[])
{
    KGset(20, 20, 10, 30, "Hi", 1, 100);
    getch();
    return EXIT_SUCCESS;
}
Degauss
  • 5
  • 4
  • Please don't start header guard macro's with underscores. See [here for the why](http://stackoverflow.com/a/228797/256138). – rubenvb Feb 26 '14 at 16:14

1 Answers1

3

You are declaring your function parameters outside your function, as though they are global objects, for some reason. Only the following should probably remain global objects:

CONSOLE_CURSOR_INFO KGcursorstat;
COORD KGbuffersize;
SMALL_RECT KGwindowsize;

Then you need to give types to your function arguments:

void KGset(int KGbuffersizex, int KGbuffersizey, int KGwindowsizex, int KGwindowsizey, TCHAR KGconsoletitle,  bool KGcursorvisible, int KGcursorsize)

Global objects (like the three above) are objects that exist for the entire duration of the program. On the other hand, parameters are objects that are passed into a function whenever it is called. You need to differentiate between these. Function parameters are declared within the parentheses after the function name.


As Chris has mentioned in the comments below, you're going to have problems with violating the One Definition Rule when you start including this header in multiple files. The reason is that each file that includes this header will attempt to define each of those global variables and the function. That leads to multiple definitions, which C++ does not allow.

Instead, your header should contain declarations, while you should have a .cpp file that contains the definitions that you've given above. To make a global variable definition into a declaration, you just put extern before it. To make a function definition into a declaration, you take away the body ({ to }) and make sure the line ends with a semi-colon.

Joseph Mansfield
  • 108,238
  • 20
  • 242
  • 324
  • Even those global objects are still particularly dangerous because they're in a header and can violate ODR. @user3356511, On the topic of bad things to put in headers, there's also using directives and include guards with [reserved identifiers](http://stackoverflow.com/questions/228783/what-are-the-rules-about-using-an-underscore-in-a-c-identifier). – chris Feb 26 '14 at 16:05
  • Come to think of it, the function definition is the same deal as the variables. – chris Feb 26 '14 at 16:19