5

I try to reference a struct from another class in my code and it gives me an error, saying I have a syntax problem.

#pragma once #include "Definitions.h" #include "GV.h" #include "UI.h" #include "Tile.h" #include "Item.h" class Figure { public: //Figure index struct FIGURE_TYPE { //Where to crop the image from SDL_Rect crop; int x; int y; }; //The game figure FIGURE_TYPE figure_index[FIGURE_COUNT]; //The figure array int figure_array[MAP_HEIGHT / 64][MAP_WIDTH / 64]; //Functions Figure ( void ); bool draw_figures ( SDL_Surface* screen, SDL_Surface* figures, SDL_Rect camera, Figure::FIGURE_TYPE figure_spec[FIGURE_COUNT] ); }; 

That's the struct in Figure.h,

#pragma once #include "Definitions.h" #include "GV.h" #include "Tile.h" #include "Item.h" #include "Figure.h" class UI { public: UI ( void ); void set_camera ( SDL_Rect& camera, Figure::FIGURE_TYPE figure_spec[FIGURE_COUNT] ); bool draw_map ( SDL_Surface* screen, SDL_Rect& camera, SDL_Surface* tiles, SDL_Surface* figures, Figure::FIGURE_TYPE figure_spec[FIGURE_COUNT] ); bool draw_status ( void ); }; 

And that is where I reference it, from another header file called UI.h. I know there is a problem with referencing structures, I just don't know how to fix it. Simple problem, any one wanna help?

The problem is not that Figure Type is declared outside of Figure.h, or that it is private as opposed to public.

Error Reports

Error 1 error C2653: 'Figure' : is not a class or namespace name c:\users\jim\documents\c++\roguelike\roguelike\ui.h 13 1 roguelike

Error 3 error C2653: 'Figure' : is not a class or namespace name c:\users\jim\documents\c++\roguelike\roguelike\ui.h 14 1 roguelike

Error 2 error C2061: syntax error : identifier 'FIGURE_TYPE' c:\users\jim\documents\c++\roguelike\roguelike\ui.h 13 1 roguelike

Error 4 error C2061: syntax error : identifier 'FIGURE_TYPE' c:\users\jim\documents\c++\roguelike\roguelike\ui.h 14 1 roguelike

11
  • 5
    What do you think Figure:: is doing? Do you think Figure is a namespace? A type? Or something else? (It's hard to correct your misunderstanding when I don't know what your reasoning is.) Commented Jan 26, 2013 at 20:21
  • I'm sorry, Figure.h contains a class declaration called Figure. I thought that Figure:: was a way to reference it. Commented Jan 26, 2013 at 20:26
  • Please include the entire error message. That message is an explanation of what's wrong and where. Commented Jan 26, 2013 at 20:27
  • I've added all of the code you should need and the error reports. Commented Jan 26, 2013 at 20:33
  • 1
    @DavidSchwartz: Figure is a class type. You're wrong. The OP's syntax and understanding is 100% correct. Commented Jan 26, 2013 at 20:37

2 Answers 2

15

You have a circular dependency: UI.h depends on Figure.h, and Figure.h depends on UI.h. You need to break the circular dependency by removing the #include of one file in the other. In this case, since I don't see anything in Figure.h using anything in UI.h, you should just remove the #include "UI.h" from Figure.h and be all set.

Sign up to request clarification or add additional context in comments.

2 Comments

+1 as your solution is more specific -- I didn't spot that the dependency isn't even required
Thank you! I have more questions about forward dependency, but I will just start a new thread as they aren't relevant to this discussion.
2

Your syntax is fine.

What is not fine is that you have a circular dependency between your headers, and this is breaking your #includes.

In this case, it's leading to Figure not being visible from within "UI.h"; even though your syntax is correct, this causes the errors you've seen because "UI.h" doesn't know that your syntax is correct, because it doesn't know what Figure is.

Do not have circular dependencies. Use forward declarations where possible to help you.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.