954,504 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Persistent errors

'lo there folks,

MinGW's giving me 1 error I can't get rid of:

eventhandler.h|9|error: `element' has not been declared|
eventhandler.h|9|error: ISO C++ forbids declaration of `elem' with no type|


All code mentioned and I think needed:

eventhandler.h

#ifndef EVENTHANDLERH
#define EVENTHANDLERH

#include "element.h"

enum events {onClick, onHover};

typedef struct eventHandlerT {
    void(*func)(element *elem);
    events eventType;
} eventHandler;


#endif // EVENTHANDLERH


element.h

#ifndef ELEMETH
#define ELEMETH

#include "eventhandler.h"

class element {
    public:
    element();
    ~element();

    void processEvent(events event);

    vector<eventHandler> handlers;
    unsigned int ID;
    static unsigned int IDcount;
};


#endif // ELEMETH

Why is element not declared? I don't get that, the header is included right above it?

Any help is greatly appreciated,

Clockowl
Posting Whiz
376 posts since May 2008
Reputation Points: 69
Solved Threads: 28
 

>>Why is element not declared?
Because you have recursive includes -- each header file includes the other.

In eventhandler.h try this:

#ifndef EVENTHANDLERH
#define EVENTHANDLERH

class element;  // forward declaration of class

enum events {onClick, onHover};

typedef struct eventHandlerT {
    void(*func)(element *elem);
    events eventType;
} eventHandler;


#endif // EVENTHANDLERH
Ancient Dragon
Retired & Loving It
Team Colleague
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
 

Thanks, it works, but...

That's where the guards (the #ifdef stuff) are for right? Not trying to be impolite, but why can't I include those headers with the guards in place?

Clockowl
Posting Whiz
376 posts since May 2008
Reputation Points: 69
Solved Threads: 28
 

>>That's where the guards (the #ifdef stuff) are for right?

Nope. The guards prevent the same include file from being processed more than once. For example:

#include "element.h"
#include "element.h" // this one will be ignored


Your program had a different problem. The preprocessor attempted to process the line that used element *elm but class element had not been fully defined yet. With forward references the class doesn't have to be fully defined in order to declare a pointer to it.

Ancient Dragon
Retired & Loving It
Team Colleague
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
 

Ah, okay. Thanks for the explanation.

Clockowl
Posting Whiz
376 posts since May 2008
Reputation Points: 69
Solved Threads: 28
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You