Results 1 to 7 of 7

Thread: redefinition?!

  1. #1
    NOMADMAN
    Guest

    redefinition?!

    I have two template classes and a regular class.
    For simplicity: TClass1, TClass2, and Class3(.h and .cpp).

    TClass2 inherites TClass1. In TClass2 I have #include "TClass1.h" And then in Class3, I #include "TClass2.h". Then once in my main.cpp I #include both "Class3.h" and "TClass2.h". Then when I compile I get a redefinition error, saying the class in TClass1 has already been redefined.
    Error:
    h:\cs131\assignment 3\grid.h(26) : error C2953: 'grid' : template class has already been defined
    (grid is the class in TClass1)

    What can I do? I need to use both TClass2 and Class3 in main. And Class3 needs to use TClass2 data type (constructor and 2 public functions).

    Anyone even know what I'm talking about?!

    NOMAD

  2. #2
    Zaei
    Guest
    Wrap your classes up with conditional preprocessor blocks:
    Code:
    #ifndef TCLASS_H
    #define TCLASS_H
    
    template<class t>
    class TClass
    {
    };
    
    #include "TClass.cpp"
    
    #endif
    Z.

  3. #3
    NOMADMAN
    Guest
    Z you are totally awsome! Thanks!

  4. #4
    NOMADMAN
    Guest

    On a related topic

    This question is related although a different question. Can I and should I be using the #ifndef/#define/#endif on regular header files that don't necessarily have a class in them. Example is one of my headers with just straight functions.

    NOMAD

  5. #5
    Fanatic Member Wynd's Avatar
    Join Date
    Dec 2000
    Location
    In a bar frequented by colossal death robots
    Posts
    772
    You probably should. For example, if you need <cstdio> in one of the functions in your header file and then need it again in your main .cpp file, you'll get redefinition errors.
    Alcohol & calculus don't mix.
    Never drink & derive.

  6. #6
    Zaei
    Guest
    I put preprocessor conditionals around all of my headers, for safety.

    Z.

  7. #7
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    Originally posted by Wynd
    You probably should. For example, if you need <cstdio> in one of the functions in your header file and then need it again in your main .cpp file, you'll get redefinition errors.
    If not for the fact that all the standard headers are guarded anyway
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width