Definition and Redefinition Errors in C++ Program

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • CodeTilYaDrop
    New Member
    • Aug 2007
    • 66

    Definition and Redefinition Errors in C++ Program

    All,

    Could someone help me with some errors. I am getting a Redefinition of 'class ClassNameHere' error in my .cpp file. Then, the header file that goes along with it has a error: previous definition of 'class ClassNameHere'. Does anyone know what this is and how to solve it? I can not seem to figure it out?

    Thanks for any help you can provide-- CTYD
  • sicarie
    Recognized Expert Specialist
    • Nov 2006
    • 4677

    #2
    You'll have to post your code to be sure, but it sounds like you have a duplicate constructor declaration (two places where you make the same declaration that takes the same arguments).

    Comment

    • CodeTilYaDrop
      New Member
      • Aug 2007
      • 66

      #3
      I figured it out after you pointed me in the right direction. I had each file starting with class ClassName. I should only have that line of code in the header section and not the definition file. I do appreciate your help with this....Thank You Sicaire- CTYD

      Comment

      • hdanw
        New Member
        • Feb 2008
        • 61

        #4
        Every time I've had this problem it is with a definition in an hpp include file.

        Definition, not declaration.

        If you include the same file twice, directly or indirectly then you have effectively typed everything into the same file twice.

        Try using the once keyword. This instructs the compiler to include the file once and no more.
        Code:
        // header.h
        #pragma once
        ....
        or use code like this
        Code:
        #ifndef SomeLabel 
        #define SomeLabel
        
        (.. Insert code here ..)
        
        #endif
        if the file has been included then "SomeLabel" is defined and the code is skipped.

        Comment

        • sicarie
          Recognized Expert Specialist
          • Nov 2006
          • 4677

          #5
          Originally posted by hdanw

          Definition, not declaration.

          Try using the once keyword. This instructs the compiler to include the file once and no more.
          Code:
          // header.h
          #pragma once
          ....
          or use code like this
          Code:
          #ifndef SomeLabel 
          #define SomeLabel
          
          (.. Insert code here ..)
          
          #endif
          if the file has been included then "SomeLabel" is defined and the code is skipped.
          Ah, good catch, and good remediation. Thanks, hdanw.

          Comment

          Working...