Results 1 to 7 of 7

Thread: Help! Redefinition Error

  1. #1

    Thread Starter
    Member
    Join Date
    May 2002
    Location
    Great Britain
    Posts
    60

    Help! Redefinition Error

    I have two classes in my project, both of which use the same header file which contains this struct definition:

    struct vertex3D {
    float X;
    float Y;
    float Z;
    };

    When I try to compile the project I get a 'struct' type redefinition error.
    How can I include the above header in both classes without getting this error?

  2. #2
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    Use #ifndef/#define/#endif for header guarding. Search the forums...
    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

  3. #3
    Frenzied Member
    Join Date
    Jul 2002
    Posts
    1,370
    like this:
    Code:
    #ifndef 3DVERTEX
    #define 3DVERTEX 1
    struct vertex3D {
    	float X;
    	float Y;
    	float Z;
    };
    
    /* all the rest of the required stuff here..... */
    #endif
    It only will be included once this way.

  4. #4
    Member
    Join Date
    Jun 1999
    Location
    Singapore
    Posts
    32
    You can use the '#pragma once' preprocessor directive. Put it at the top of your header files. It tells the compiler to only include the header file once.

  5. #5
    Junior Member
    Join Date
    Oct 2002
    Location
    MI
    Posts
    23
    I really should learn to use those preprocessor macros...

  6. #6
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    Originally posted by atjs
    You can use the '#pragma once' preprocessor directive. Put it at the top of your header files. It tells the compiler to only include the header file once.
    That's only for MSVC.
    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

  7. #7
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    Originally posted by parksie
    That's only for MSVC.
    Which is ok if the headers are guaranteed to be only used by MSVC (like headers of MFC apps), but not if the headers contain general utility functions/classes.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

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