943,545 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Marked Solved
  • Views: 6560
  • C++ RSS
Sep 12th, 2007
0

Help: 'error: redefinition'

Expand Post »
Hello there, I have here a problem with the compilation of my program. The compiler tells me that my typedefs are redefined, but I defined it only once. Ok, heres the score, I have here a header file (let's name this as file X) that contains all the typedefs that I need (so that I will be able to reuse it to other files). I have included it to the two files (let's say, file A and file B) that really needs it badly and these two files (file A and B) are included to a file that needs them too (let's call this one as file C). Whenever I compile file C, I get the error of redefinition. Is there something wrong with the way I mess with the files? I'm using gcc thus involving Makefile.

Thanks!
Similar Threads
Reputation Points: 32
Solved Threads: 4
Practically a Master Poster
jaepi is offline Offline
647 posts
since Jul 2006
Sep 12th, 2007
0

Re: Help: 'error: redefinition'

you header file need code gards to prevent that, like this
C++ Syntax (Toggle Plain Text)
  1. #ifndef MYHEADER
  2. #define MYHEADER
  3. // code goes here
  4.  
  5. #endif
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,947 posts
since Aug 2005
Sep 12th, 2007
1

Re: Help: 'error: redefinition'

You could also use #pragma once but it is compiler dependent and deprecated. Its generally used with MSVC++.
Reputation Points: 85
Solved Threads: 42
Nearly a Posting Virtuoso
vishesh is offline Offline
1,362 posts
since Oct 2006
Sep 12th, 2007
1

Re: Help: 'error: redefinition'

>You could also use #pragma once but it is compiler dependent and deprecated.
All pragmas (well, most with the coming of C99) are compiler dependent. You should avoid them whenever possible. And if #pragma once is compiler dependent, how can you say that it's deprecated? Have all compilers documented it as deprecated? Anyway, the only real advantage of #pragma once is that you don't have to worry about naming collisions in your inclusion guards, and that's not worth the loss of portability.
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
Sep 12th, 2007
0

Re: Help: 'error: redefinition'

Click to Expand / Collapse  Quote originally posted by Narue ...
>You could also use #pragma once but it is compiler dependent and deprecated.
All pragmas (well, most with the coming of C99) are compiler dependent. You should avoid them whenever possible. And if #pragma once is compiler dependent, how can you say that it's deprecated? Have all compilers documented it as deprecated? Anyway, the only real advantage of #pragma once is that you don't have to worry about naming collisions in your inclusion guards, and that's not worth the loss of portability.
Of course all pragmas are compiler dependent and different compilers could have different pragmas. I didn't mentioned it all but I think its pretty clear. By deprecated I meant its somewhat strongly not advised. I have written what I have read and inferred from it sometime. somewhere.
Reputation Points: 85
Solved Threads: 42
Nearly a Posting Virtuoso
vishesh is offline Offline
1,362 posts
since Oct 2006
Sep 12th, 2007
0

Re: Help: 'error: redefinition'

I placed #pragma once in file X and it worked. Thanks guys.
Reputation Points: 32
Solved Threads: 4
Practically a Master Poster
jaepi is offline Offline
647 posts
since Jul 2006
Sep 13th, 2007
0

Re: Help: 'error: redefinition'

Didn't this worked as suggested by Ancient Dragon . Its better to use that and you must know how to use it
  1. #ifndef MYHEADER
  2. #define MYHEADER
  3. // code goes here
  4.  
  5. #endif
Last edited by vishesh; Sep 13th, 2007 at 6:17 am.
Reputation Points: 85
Solved Threads: 42
Nearly a Posting Virtuoso
vishesh is offline Offline
1,362 posts
since Oct 2006

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: decimal to binary conversion
Next Thread in C++ Forum Timeline: project





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC