Thread: Class Redefinition error

  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
    28,413
    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.
    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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 11-15-2010, 11: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