Thread: redeclaration of enumerator

  1. #1
    Registered User
    Join Date
    Jan 2009
    Posts
    2

    redeclaration of enumerator

    Hello Experts,

    I have two headers "common.h" and "libsomething.h" both define an enumeration called "OK" and neither belong to me (they are from different projects). I need to reference both these headers in one of my own. When I compile I get the following error:

    error: redeclaration of enumerator ‘OK’

    Is there a way for me to take advantage of the code in both these files?

    Thanks

  2. #2
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    I guess you will have to rename one of them.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  3. #3
    Registered User
    Join Date
    Jan 2009
    Posts
    2
    As I mentioned neither belong to me, they are part of other projects one opensource and the other commercial.

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Change the open source code? OK is a rather bad name on two counts: it is short and without some kind of prefix (in a language without namespaces!), and it is all in uppercase which is a convention normally reserved for macro names.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Do you need both definitions of OK?

    If not, something like this might work
    Code:
    #define OK BAH_HUMBUG
    #include "common.h"
    #undef OK
    #include "libsomething.h"
    If you do need both, then you'll have to create your own small wrapper API which results in you only #including one of the header files at once.
    That can get a bit messy if you're not careful.

    If you only want say a small part of libsomething.h (say), and most of common.h, then you might also be able to just wrap the parts of libsomething which really interest you.

    Also, raise a bug report on both packages telling them about the poor use of generic names like "OK", and the likely conflicts it generates.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. string redeclaration in function, why is required?
    By elninio in forum C++ Programming
    Replies: 6
    Last Post: 07-14-2008, 06:58 PM
  2. Casting Enumerator?!?
    By mdoland in forum C# Programming
    Replies: 1
    Last Post: 12-10-2007, 05:43 AM
  3. C datatype, enumerator
    By onebrother in forum C Programming
    Replies: 3
    Last Post: 07-26-2007, 12:49 AM
  4. load gif into program
    By willc0de4food in forum Windows Programming
    Replies: 14
    Last Post: 01-11-2006, 10:43 AM
  5. Redeclaration Error
    By shiju in forum C Programming
    Replies: 6
    Last Post: 09-02-2004, 07:51 AM

Tags for this Thread