Class inheritance problems

This is a discussion on Class inheritance problems within the C++ Programming forums, part of the General Programming Boards category; Hi there! First time on this forum, guys from the SDL mailing list gave me the tip Im having a ...

  1. #1
    Registered User
    Join Date
    Aug 2006
    Posts
    4

    Post Class inheritance problems

    Hi there!
    First time on this forum, guys from the SDL mailing list gave me the tip

    Im having a problem with the 2D graphics game engine Im developing. Im
    doing the event management system right now and decided that I should create a class named EventHandler that has a pure virtual function named
    HandleEvent. This function is the one that, obviously, handles inputs and
    any other ocurrencies differently depending on the element of the game we
    are talking about, like a sprite, a tile, or, talking high-level, items,
    characters and so on.
    Many classes must inherit from EventHandler so they are obliged to implement
    HandleEvent() and can be all referenced on a single vector that I will use
    later to check some things out.
    BUT the problem is: I have every class on a header file, and its function
    codes on a correspondent .cpp file. So each of these header files supposedly
    must include my 'eventHandler.h', causing multiple referencing. And I cant
    just use forward referencing to EventHandler, because of the inheritance.
    What do I do? Im using Dev-C++, and all my header files have those #ifndef
    #define #endif directives.

    Thanks in advance,
    Carol

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,275
    BUT the problem is: I have every class on a header file, and its function
    codes on a correspondent .cpp file. So each of these header files supposedly
    must include my 'eventHandler.h', causing multiple referencing. And I can´t
    just use forward referencing to EventHandler, because of the inheritance.
    What do I do? I´m using Dev-C++, and all my header files have those #ifndef
    #define #endif directives.
    If eventHandler.h uses header guards as you say your header files have, then there should not be a problem. Why not give a minimal example that shows what is the problem that you are concerned with?
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Aug 2006
    Posts
    4

    Post Here is some code

    'event.h' is where all the wrongly referenced code is. 'tilemap.h' is just one of the files that reference to 'event.h', but they all do it the same way (simply using #include "event.h"). The error message is:

    multiple definition of `EventHandler::CleanEvent()'
    first defined here
    multiple definition of `EventHandler:ispatchEvent()'
    first defined here
    multiple definition of `EventHandler::FillEvent(_eventID, EventHandler*, EventHandler*)'
    first defined here
    multiple definition of `EventHandler::FillEvent(_eventID, EventHandler*, bool)'
    first defined here

    The phrases above repeat themselves more 6 times(number of references to 'event.h'), then comes:

    [Linker error] undefined reference to `vtable for EventHandler'
    [Linker error] undefined reference to `vtable for EventHandler'

    This one also comes around 7 times:

    [Linker error] undefined reference to `EventHandler::HandleEvent()'
    Attached Files Attached Files

  4. #4
    ZuK
    ZuK is offline
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    These are linker errors.
    You should not define the member functions (like `EventHandler::CleanEvent() ) in the header. If you think that you have to, use the inline keyword or define them inside the class.
    Kurt
    Last edited by ZuK; 08-25-2006 at 04:54 AM.

  5. #5
    Registered User
    Join Date
    Aug 2006
    Posts
    4

    Talking Thanks it worked!

    So THATs what inline functions are for.

    Thanks,
    Carol =)

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,275
    By the way, it is good practice for classes intended as base classes to have a virtual destructor.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Registered User
    Join Date
    Aug 2006
    Posts
    4
    Didnt know that. Ill do some research on it then.

    Thanks again!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 10-19-2008, 01:10 PM
  2. Inheritance problems
    By wheels165 in forum C++ Programming
    Replies: 3
    Last Post: 02-23-2004, 04:49 PM
  3. Back with some inheritance fun
    By hpy_gilmore8 in forum C++ Programming
    Replies: 11
    Last Post: 01-17-2004, 12:15 AM
  4. Need some help on class inheritance
    By HelpMe in forum C++ Programming
    Replies: 1
    Last Post: 05-21-2002, 04:44 PM
  5. gcc problem
    By bjdea1 in forum Linux Programming
    Replies: 13
    Last Post: 04-29-2002, 07:51 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21