Question

I have a header, and it includes a prototype of a structure:

struct UserDataStruct;

The source file for the header contains and include for the header and:

struct UserDataStruct
{
    int instance;
};

In my main source file, I have:

int main ()
{
    UserDataStruct lol;

    return 0;
}

I can't seem to compile my program because Visual Studio 2010 gives me an error:

error C2079: 'lol' uses undefined struct 'UserDataStruct'

How can I make this work?

Was it helpful?

Solution

The complete definition of UserDataStruct needs to come before you create an instance of it. Forward declares aren't enough in this case.

Just move your struct definition from the .cpp file to the header.

OTHER TIPS

This won't work; the structure needs to be completely know in main(); you must put the full declaration of your structure in the header file, then include it in the main .c file.

You can only forward declare structures/unions/classes (because that is what you are doing) if you use pointers in other header files. But eventually you will have to provide the full declaration in your .c files. You can't "hide" the internals of your structure.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top