Hello, I am writing a small console game to get back into the spirit of programming. However, I seem to have small issue. I have created a namespace and I am trying to include it from another file. Here is my code:
console.h
1 2 3 4 5 6
namespace console {
/* declare the prototype to move the cursor */
externvoid moveCursorX(int);
}
#endif
#ifdef _WIN32
#include <Windows.h>
#endif
#ifdef __linux__
#include <ncurses.h>
#endif
#ifndef CONSOLE_CPP
#define CONSOLE_CPP
namespace console {
/* declare the function to move the cursor */
void moveCursorX(int x) {
#ifdef _WIN32
/* get the most recent output handle and information */
HANDLE output = GetStdHandle(STD_OUTPUT_HANDLE);
CONSOLE_SCREEN_BUFFER_INFO csbi;
GetConsoleScreenBufferInfo(output, &csbi);
/* get the current position of the console cursor */
COORD position;
position.X = csbi.dwCursorPosition.X;
position.Y = csbi.dwCursorPosition.Y;
/* assign the x console cursor position from the parameter */
position.X = x;
/* update the current console cursor position */
SetConsoleCursorPosition(output, position);
#endif
#ifdef __linux__
/* not implimented yet! */
#endif
}
}
#endif
I am not sure why, but the code will not compile unless I have both the .h and .cpp file included. Normally, when I am working on classes, I include the .h in the .cpp. I then include the .h in whatever file I need to use that class in. Could someone explain why that isn't the case here?
You have to compile both .cpp files and then link the result together into an executable file. If you use an IDE much of this is probably done automatically if you just make sure to have all files added to the same project.