C++ struct in separate header file -
this question has answer here:
- headers including each other in c++ 7 answers
i've tried organize project thought include of global variables, #includes , struct definition in global.h header. can't understand concept , errors during build seem prove that. when try access global.h in logic.h happens.
global.h:
#ifndef global_h #define global_h #include "logic.h" #include <sdl.h> #include <iostream> #include <string> #include "graphics.h" //global variables , structs enum directions { d_up, d_left, d_down, d_right, d_total }; struct character { float health; float x; float y; float velocity; bool collision[d_total]; directions direction; sdl_rect animation; sdl_rect sprite; }; const int windowwidth = 800; const int windowheight = 600; const int framewidth = 64; const int frameheight = 64; #endif // global_h
logic.h:
#include "global.h" //header gamelogic functions //initialization of variables new character void initcharacter(character &newcharacter, float x, float y, directions startingdirection);
when try build error i'm getting:
||=== build: debug in gameproject0.2 (compiler: gnu gcc compiler) ===| c:\users\rafał\documents\gameproject0.2\gameproject0.2\logic.h|5|error: variable or field 'initcharacter' declared void| c:\users\rafał\documents\gameproject0.2\gameproject0.2\logic.h|5|error: 'character' not declared in scope| c:\users\rafał\documents\gameproject0.2\gameproject0.2\logic.h|5|error: 'newcharacter' not declared in scope| c:\users\rafał\documents\gameproject0.2\gameproject0.2\logic.h|5|error: expected primary-expression before 'float'| c:\users\rafał\documents\gameproject0.2\gameproject0.2\logic.h|5|error: expected primary-expression before 'float'| c:\users\rafał\documents\gameproject0.2\gameproject0.2\logic.h|5|error: 'directions' not declared in scope| ||=== build failed: 6 error(s), 0 warning(s) (0 minute(s), 0 second(s)) ===|
i not sure i'm missing. advice!
when include "global.h"
, 1 of first things says include "logic.h"
, processor opens file, finds include "global.h"
, nothing because of include guard, finds void initcharacter(character &newcharacter,
, gets confused because doesn't yet know character
is. returns include, lexes enum directions
, finally lexes struct character
, then knows character
is, far late.
in general, try make sure includes go 1 way. make sure logic
includes global
, or global
includes logic
, not both.
or, if you're lazy, there's trick sidesteps issue: put function , class definitions before includes, possible. global
tell compiler class character
exists, when includes logic.h
, won't have issue.
#ifndef global_h #define global_h enum directions; struct character; //if you're lazy, works #include "logic.h" #include <sdl.h> #include <iostream> #include <string> #include "graphics.h" //global variables , structs enum directions { d_up, d_left, d_down, d_right, d_total }; struct character { ...
Comments
Post a Comment