Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using a struct in a header file "unknown type" error

I am using Kdevelop in Kubuntu. I have declared a structure in my datasetup.h file:

#ifndef A_H
#define A_H

struct georeg_val {

    int p;
    double h;
    double hfov;
    double vfov;
};

#endif

Now when I use it in my main.c file

int main()
{
    georeg_val gval;

    read_data(gval); //this is in a .cpp file

}

I get the following error:

georeg_chain.c:7:3: error: unknown type name 'georeg_val'

(This is in the georeg_val gval; line)

I would appreciate if anyone could help me resolve this error.

like image 434
dead_jake Avatar asked Apr 30 '12 14:04

dead_jake


People also ask

Can you put a struct in a header file?

If a struct is declared in a header file in C++, you must include the header file everywhere a struct is used and where struct member functions are defined. The C++ compiler will give an error message if you try to call a regular function, or call or define a member function, without declaring it first.

Can you define a struct in a header file C++?

You can't. In order to "use" the struct, i.e. to be able to declare objects of that type and to access its internals you need the full definition of the struct. So, it you want to do any of that (and you do, judging by your error messages), you have to place the full definition of the struct type into the header file.

What is a typedef struct?

typedef is a predefined keyword. You can replace the name of the existing data type with the name which you have provided. This keyword helps in creating a user-defined name for an existing data type. You can also use the typedef keyword with structures, pointers, arrays etc.


2 Answers

In C one has two possibilities to declare structure:

struct STRUCT_NAME {} ;

or

typedef struct {} STRUCT_ALIAS;

If you use first method (give struct a name) - you must define variable by marking it explicitly being a struct:

struct STRUCT_NAME myStruct;

However if you use second method (give struct an alias) then you can omit struct identifier - compiler can deduce type of variable given only it's alias :

STRUCT_ALIAS myStruct;

Bonus points: You can declare struct with both it's name and alias:

typedef struct STRUCT_TAG {} STRUCT_TAG;
// here STRUCT_NAME == STRUCT_ALIAS

Then in variable definition you can use either first or second method. Why both of two worlds is good ? Struct alias lets you to make struct variable definitions shorter - which is a good thing sometimes. But struct name let's you to make forward declarations. Which is indispensable tool in some cases - consider you have circular references between structs:

struct A {
  struct B * b;
}
struct B {
  struct A * a;
}

Besides that this architecture may be flawed - this circular definition will compile when structs are declared in the first way (with names) AND struct pointers are referenced explicitly by marking them as struct.

like image 143
Agnius Vasiliauskas Avatar answered Sep 28 '22 08:09

Agnius Vasiliauskas


If you have to define a new type, you have to write:

typedef struct {

    int p;
    double h;
    double hfov;
    double vfov;
} georeg_val ;

Then you can use georeg_val as a new type.

like image 37
Gabriel Avatar answered Sep 28 '22 08:09

Gabriel