James Hurley
James Hurley

Reputation: 762

Error C2061 In visual studio

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

Upvotes: 5

Views: 32664

Answers (2)

Lightness Races in Orbit
Lightness Races in Orbit

Reputation: 385405

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.

Upvotes: 2

Adam Rosenfield
Adam Rosenfield

Reputation: 400682

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.

Upvotes: 15

Related Questions