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

//Figure index
struct FIGURE_TYPE {
    //Where to crop the image from
    SDL_Rect crop;
    int x;
    int y;
};

That's the struct in Figure.h,

void set_camera ( SDL_Rect& camera, Figure::FIGURE_TYPE figure_index[FIGURE_COUNT] );

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?

Recommended Answers

All 5 Replies

Making your FIGURE_TYPE struct from your Figure class public can solve your problem. Presumably your FIGURE_TYPE is in the defauls private block of your class, which means you can't access it directly. Making it public allows other classes to use that struct, even if it's declared in the Figure class.
Here's a small example:

#include <iostream>
using namespace std;
#define NR 5

class SDL_Rect{};
class A{
public:
    struct FIGURE_TYPE {
        SDL_Rect crop;
        int x;
        int y;
    };
};

class B{
public:
    void printFig(A::FIGURE_TYPE figure_index[NR]){
        for (int i=0;i<NR;i++){
            cout<<"X: "<<figure_index[i].x<<" Y: "<<figure_index[i].y<<endl;
        }
    }
};

int main(){
    A::FIGURE_TYPE figs[NR];
    for (int i=0;i<NR;i++){
        figs[i].x=i+10;
        figs[i].y=i+20;
    }
    B().printFig(figs);
    return 0;
}

It is public. Any other ideas?

Thank you for taking an interest, by the way.

This might be basic, but have you included UI.h in the current file?

It turns out I had a circular dependency with my headers. Thank you for helping though!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.