undeclared identifier error (SOLVED)

Started by
5 comments, last by Charles Thomas 18 years, 10 months ago
For simplicity, lets say I have 3 files: main.cpp, event.h, eventcontroller.h event.h contains a class cEvent, which uses eventcontroller's singleton eventcontroller.h contains a class cEventController, which uses a pointer of cEvent main.h contains the main function, and does nothing beside includes event.h/eventcontroller.h I am getting an error in eventcontroller.h, saying cEvent is an undeclared identifier. I'm assuming this is because I use cEvent in class cEventController and the compiler has not completed event.h yet. How can I solve this without placing the classes in one file? [Edited by - Charles Thomas on June 8, 2005 11:56:07 AM]
Advertisement
You can forward declare classes in header files. Take this code:-

Event.h

#ifndef __EVENT_H__#define __EVENT_H__#include "EventManager.h"class CEvent {public:void Func( CEventManager* pManger );}; // end class CEvent#endif __EVENT_H__


EventManager.h

#ifndef __EVENTMANAGER_H__#define __EVENTMANAGER_H__#include "Singleton.h"// forward declare the event classclass CEvent;class CEventManager : public CSingleton< CEventManager > {public:void Func( CEvent* pEvent );}; // end class CEvent#endif __EVENTMANAGER_H__


Notice how CEvent was forward declared in EventManager.h?

In order to make this code work, make sure that EventManager.cpp defines the CEventManger::Func method and includes Event.h.

This should now work.
foward declare the cEvent class like this:

class cEvent;

this class will now be an incomplete class and can only be used in certain ways but you can declare a pointer to one. see 3.2 paragraph 4 in http://www.kuzbass.ru:8086/docs/isocpp/basic.html#basic.def.odr about when a type needs to be 'complete'.
Sounds like you've got a re-entrant linking problem. One way would be to simply declare a forward class reference to cEvent in eventcontroller.h

ie:
// eventcontroller.h#pragma once#ifndef EVENTCONTROLLER_H#define EVENTCONTROLLER_H// forward refclass cEvent;class cEventController {...};#endif


Generally speaking, as long as you're not deriving from cEvent, calling any of it's members in the header, or creating a non-pointer member variable of that type, you do not need to explicitly include it's header.

Of course, I'm just assuming this is what's happening based on your description. Source code would be nicer.
in eventcontroller.h declare the event class before the controller class.
/* eventcontroller.h */class cEvent;/* class cEventController definition here */

edit: wow i'm slow today...
This space for rent.
Quote:Original post by pragma Fury
As long as you're not deriving from cEvent, calling any of it's members in the header, or creating a non-pointer member variable of that type, you do not need to explicitly include it's header.


there are actually more cases then this. for example sizeof or typeid can not be applied to cEvent. see the link i posted above.

Excellent! It works, thank you for the help.

This topic is closed to new replies.

Advertisement