Class Redefinition error

This is a discussion on Class Redefinition error within the C++ Programming forums, part of the General Programming Boards category; Hello, I have a class in ClassManager.h declared as follows: Code: #include "DebugWriter.h" class OutputManager : public DebugWriter { The ...

  1. #1
    Registered User
    Join Date
    Jul 2011
    Posts
    27

    Class Redefinition error

    Hello,
    I have a class in ClassManager.h declared as follows:

    Code:
    #include "DebugWriter.h"
    
    class OutputManager : public DebugWriter {
    The class uses DebugWriter class defined in DebugWriter.h.
    The compiler runs fine, and all intended effects of DebugWriter is working. Now I have a new classes defined as follows:


    Code:
    #include "DebugWriter.h"
    
    class BMManager : public DebugWriter {

    Once added I have class DebugWriter redefiniton error. I got rid of the error after adding #ifndef.
    Question:
    I am trying to understand how the compiler runs the pass. I am thinking it needs to compile
    step 1: compiler compiles OutputManager.cpp to OutputMaqnger.o and does it that by loading up OutputManger.cpp, then loads up OutManager.h and then loads DebugWriter.h.

    Step 2:
    compiler compiles BMManager.cpp to BMManager.o and does it that by loading up BMManger.cpp, then loads up BMManager.h and then loads DebugWriter.h.


    Step 3: The .o objects is linked to become .exe


    Question:
    In step 2, the compiler should have no prior knowledge that DebugWriter class is defined. Why would it says redefined??

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,753
    Quote Originally Posted by derder
    I got rid of the error after adding #ifndef.
    Yes, always use inclusion guards when writing headers.

    Quote Originally Posted by derder
    In step 2, the compiler should have no prior knowledge that DebugWriter class is defined. Why would it says redefined??
    It shouldn't. However, since it did say that, and since you are more likely to have made a mistake, you probably forgot to account for a multiple inclusion of DebugWriter.h through another header that was included.
    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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 11-15-2010, 10:14 AM
  2. Redefinition of class
    By bijan311 in forum C++ Programming
    Replies: 5
    Last Post: 10-17-2010, 03:43 PM
  3. Replies: 8
    Last Post: 10-02-2005, 12:27 AM
  4. Should call redefinition error but isn't.
    By drb2k2 in forum C++ Programming
    Replies: 0
    Last Post: 04-16-2003, 05:18 AM
  5. Redefinition Error
    By Unregistered in forum C++ Programming
    Replies: 1
    Last Post: 05-04-2002, 04:11 PM

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