![]() ![]() If you've read it and still can't solve your problem, then you haven't really understood it yet, and need to read it again. You really, really must read and understand all of this in full. It doesn't matter what order you compile the source files in, because one (or more) of them causes a problem - by itself, because of the results of the preprocessing - and sooner or later you'll have to compile it. But changing the order that the source files are compiled won't fix your problem with header organization. The actual source files are compiled in the order you tell it to compile them in (at the command line or via a makefile or via some configuration stuff in your IDE). ![]() It's just a technique for using the commands that the preprocessor knows (#ifndef, #define, #endif - there are others, too) to acheive the desired effect. There is nothing magical about the #ifndef SOMETHING / #define SOMETHING / blah blah blah / #endif pattern. ![]() The header guards are designed to stop headers from being pasted more than once into the same source, because the result would confuse the compiler ('which "CGameplay" are you talking about the one at line 23 or the one at line 142?'). Then the result of all of that is fed to the actual compiler. Starting with some given source file, the preprocessor looks up the file mentioned for each #include line, the code from that file is preprocessed (recursively), and then the result replaces the #include line. It literally copies and pastes headers to replace the corresponding #include lines. In what order exactly does the compiler compile files #ifndef INC_CURSOR_H#define INC_CURSOR_H#include "engine.h"class CCursor #endif //INC_GAMEPLAY_HĪs for the included files not mentioned, "engine.h" includes "game.h" and all my DX includes (input, graphics), "game.h" includes "engine.h" and "gameplay.h", but all of them are inclusion guarded. What I'm stumped with is why the compiler doesn't register that I'm using CCursor, when just a couple of lines above it I include the header with the class definition.Īnd this solution just seems really stupid, first including the header, then putting a forward declaration, and then using it as a pointer, when logically I would just include the header and off I go.Įvery header I write has inclusion guards, and each of them is defined exactly as it's name is (gameplay.h has INC_GAMEPLAY_H, cursor has INC_CURSOR_H etc). The CGameplay header looks something like this:Ĭompiling this gives me 3 errors, one C2146 (missing ' ' before identifier 'm_cursor') and two C4430 (missing type specifier), all on the same line where the CCursor is declared inside CGameplay.Īdding "class CCursor " (forward declaration) just after the #include "cursor.h" line gives me the error C2079 ('CGameplay::m_cursor' uses undefined class 'CCursor').Ĭhanging the declaration to be a pointer instead of a normal variable solves the problem. ![]() Then I've made a CGameplay class which would be where all the game code happens - not really important right now. I've made a class CCursor, which I've put in it's own header file, which on it's own includes a header for DX stuff (I'm using LPDIRECT3DTEXTURE9 in it). I have eventually resolved the problem, but in a way that seems _really_ stupid to me. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |