Thread: Class inheritance problems

  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

    I´m having a problem with the 2D graphics game engine I´m developing. I´m
    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 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.

    Thanks in advance,
    Carol

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    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?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    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()'

  4. #4
    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 03:54 AM.

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

    Talking Thanks it worked!

    So THAT´s what inline functions are for.

    Thanks,
    Carol =)

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    By the way, it is good practice for classes intended as base classes to have a virtual destructor.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Registered User
    Join Date
    Aug 2006
    Posts
    4
    Didn´t know that. I´ll 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, 12: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, 03:44 PM
  5. gcc problem
    By bjdea1 in forum Linux Programming
    Replies: 13
    Last Post: 04-29-2002, 06:51 PM