Thread: Have an error that i can't solve

  1. #1
    Registered User
    Join Date
    Apr 2011
    Posts
    17

    Have an error that i can't solve

    redefinition of 'class Data'
    previous definition of 'class Data'

    ---note--- Data is a class that i declare in a header file

  2. #2
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    you probably don't have include guards. you need to either say

    Code:
    #pragma once
    
    or
    
    #ifndef MY_HEADER_FILE
    #define MY_HEADER_FILE
    
    class Data
    {
    
    };
    
    #endif // MY_HEADER_FILE

  3. #3
    Registered User
    Join Date
    Apr 2011
    Posts
    17
    thanks it worked, but what exactly are include guards and what instance do you need to use them

  4. #4
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    Usually they are used on the top and bottom of a file. Think for example a common header file, like <stdlib.h>. You might included it in a lot of files. If you use guards, it will just use it once, which is your intention, the compiler just doesn't do this automatically for you.

    To use it to skip a piece of code can be tricky and I won't recommend it. You might have two Data class, but different. In this case you should use namespaces for example or rename one to solve your problem. If you have to have the exact same class, then you should write it on a separate file and include it when needed.

    Technically speaking, what happens is that the pre-compiler will check if MY_HEADER_FILE variable is defined. If it is it will scroll down to #endif. So it will skip the whole Data class. If it is not defined it will #define it. Now, if you have another header file with the same Data class, it will skip it, giving no errors. So when used on a whole file, you simply skip the whole file.

  5. #5
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    How does
    Code:
    #pragma once
    work ?

  6. #6
    Registered User
    Join Date
    Feb 2003
    Posts
    596
    #pragma once does basically the same thing as include guards, except that it is non-standard, i.e. is only supported by specific compilers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. solve this error
    By moussa in forum C Programming
    Replies: 3
    Last Post: 05-28-2008, 05:38 AM
  2. how to solve this error msg
    By rhythm in forum C++ Programming
    Replies: 11
    Last Post: 02-08-2008, 10:05 PM
  3. error cant solve
    By developer47 in forum C Programming
    Replies: 1
    Last Post: 04-30-2007, 05:10 AM
  4. Cant solve this error... please help...
    By Goosie in forum C++ Programming
    Replies: 16
    Last Post: 07-08-2005, 10:32 PM
  5. please help me to solve the error
    By Jasonymk in forum C++ Programming
    Replies: 8
    Last Post: 05-02-2003, 09:08 AM