Redefinition of classes (#include headers)

This is a discussion on Redefinition of classes (#include headers) within the C++ Programming forums, part of the General Programming Boards category; What is the best way to avoid the: 'class' type redefinition errors? This comes from anytime I include a header ...

  1. #1
    Registered User
    Join Date
    May 2003
    Posts
    38

    Redefinition of classes (#include headers)

    What is the best way to avoid the: 'class' type redefinition errors? This comes from anytime I include a header for a file that has already been included somewhere else. First of all, why is this a problem, and second, what a quick and easy way to fix this.

    thanks

    -from looking at previous post it looks like my answer has something to do with either "using namespace std" or "#ifndef"
    chris

  2. #2
    'AlHamdulillah
    Join Date
    Feb 2003
    Posts
    790
    for example, a main.h file
    Code:
    #ifndef _MAIN_H
    #define _MAIN_H
    
    //...............code here
    
    #endif

  3. #3
    Even death may die... Dante Shamest's Avatar
    Join Date
    Apr 2003
    Location
    Malaysia
    Posts
    970
    First of all, why is this a problem
    You can't declare or define things twice.

    and second, what a quick and easy way to fix this.
    Use inclusion guards, as EvBladeRunnervE has shown.

  4. #4
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Posts
    5,439
    To add to ' EvBladeRunnervE' post...

    Code:
    #ifndef _MAIN_H
    #define _MAIN_H
    
    #ifndef _STDIO_H
    #include <stdio.h>
    #endif 
    
    #ifndef _STDLIB_H
    #include <stdlib.h>
    #endif 
    
    //...............code here
    
    #endif // !_MAIN_H
    Code:
    int main(void){srand(time(0));for(double l=rand(),l0=0,l00=0;;l0+=0.1){for(double l000=0;l000
    <1;l000+=.001,l+=((double)rand()/RAND_MAX)/0x64,l00+=((sin(l*0x8*atan(l0)*l000-(l0*0x8*atan
    (l)))*0.5)+0.5)){l00-=floor(l00);for(size_t l0000=0,l00000=(size_t)(0x50*(l00));l0000<l00000;++l0000
    )putchar(0x20);putchar(0x61+(int)((double)rand()/RAND_MAX*0x1a));putchar('\n');}}return 0;}

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. does not name a type ERROR
    By DarrenY in forum C++ Programming
    Replies: 3
    Last Post: 10-13-2007, 04:54 AM
  2. classes, inheritance and include problems
    By baniakjr in forum C++ Programming
    Replies: 6
    Last Post: 12-12-2006, 12:45 PM
  3. Headers and Classes
    By MMD_Lynx in forum Game Programming
    Replies: 2
    Last Post: 12-01-2004, 09:40 AM
  4. Headers, Classes, and Functions question.
    By Zeusbwr in forum C++ Programming
    Replies: 1
    Last Post: 10-21-2004, 03:18 AM
  5. How do I include Headers and Libs with Borland 3.1?
    By fatpotatohead in forum C Programming
    Replies: 2
    Last Post: 09-23-2001, 02:27 PM

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