2

I'm sure its something very foolish, but I'm stumped:

I have a typedef'd struct declared in one header:

Firstheader.h

typedef struct Pin
{ 
    uint8_t a;
    uint8_t b;
} PinStruct;

I then want to use this typedef'd struct in a function declaration in another header:

Secondheader.h

#include "Firstheader.h"

void foo (const PinStruct *myPin);

Despite the typedef'd struct being clearly defined in the first header, and including the first header in the second header, the compiler complains that the "identifier PinStruct is undefined". Someone please smack me and tell me what I'm missing.

EDIT: Thanks for the comments guys. The compiler is marking the function declaration in the second header as the source of the error. The exact error is just as I wrote: Error[Pe020]: identifier "PinStruct" is undefined.

What's strange is if I copy the struct definition into the Secondheader.h header file, the compiler immediately complians about a re-definition of the struct. So it knows its there.

Haris
  • 12,120
  • 6
  • 43
  • 70
nobby
  • 373
  • 1
  • 3
  • 15
  • 11
    That should compile fine. You'll need to construct a [minimal test-case](http://sscce.org), along with the *exact* error message. – Oliver Charlesworth Aug 13 '13 at 20:38
  • This should work? Is it complaining in the header or some other piece of code? – Jesus Ramos Aug 13 '13 at 20:38
  • 1
    Care to paste the **exact** error message you're getting?? You may do well to also ensure the headers you think are being included are in fact so. `#error` or `#pragma message` are both great temp-ways to test that. – WhozCraig Aug 13 '13 at 20:45
  • 1
    I think this might have your answer http://stackoverflow.com/questions/5343916/include-a-header-on-another-header-file – Tore Aug 13 '13 at 20:46
  • What are the last several lines of `Firstheader.h`? Sometimes, when an error is reported on a line after a `#include` line, the actual error is coming from the end of the included header file. – Adam Rosenfield Aug 14 '13 at 18:24
  • You probably have a circular dependency between headers – Jonathan Wakely Aug 14 '13 at 18:41

3 Answers3

0

Your code should work perfectly fine (tested with both GCC and CLANG).

  1. You are defining your structure within your typedef and aliasing it to PinStruct -- no errors.
  2. You include the first header file -- no errors.
  3. You use PinStruct in your function declaration of foo -- no errors.

The error could be in your C source file, have you included the header file of "file 2" in the C source file of "file 2"? Example:

// File file_1.h
typedef ...

... and:

// File file_2.h
#include "file_1.h"

// Function declaration.

... and lastly (error here):

// File 2.c
#include "file_2.h" // Forgot?
Jacob Pollack
  • 3,703
  • 1
  • 17
  • 39
0

The syntax is fine. Make sure you don't have grammar errors anywhere. Also when you are prototyping, you don't need to declare the variable name there. You can just do

void foo(const PinStruct *);
Ion
  • 334
  • 3
  • 15
0

You haven't provided enough information, so your question should be closed, but what this usually means is that FirstHeader.h somehow ends up including SecondHeader.h before PinStruct gets declared - i.e. a circular dependency.

You can solve it by declaring PinStruct in SecondHeader.h, using a so-called forward declaration:

typedef struct Pin PinStruct;
Jonathan Wakely
  • 166,810
  • 27
  • 341
  • 521