Jump to content
  • Advertisement
Sign in to follow this  
Charles Thomas

undeclared identifier error (SOLVED)

This topic is 4733 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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]

Share this post


Link to post
Share on other sites
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 class
class 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.

Share this post


Link to post
Share on other sites
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'.

Share this post


Link to post
Share on other sites
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 ref
class 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.

Share this post


Link to post
Share on other sites
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...

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!