Header File Confusion

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 */
	extern void moveCursorX(int);
}

#endif 


console.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#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 


main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
#include <iostream>

#include "console.h"
#include "console.cpp"

int main (void) {
	console::moveCursorX(10);
	
	std::cout << "Hello world" << std::endl;

	std::cin.get();
	return 0;
}


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?

Thanks!
Last edited on
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.
Topic archived. No new replies allowed.