Thread: Enumeration problem

  1. #1
    Registered User
    Join Date
    Sep 2005
    Posts
    20

    Question Enumeration problem

    i've got such problem.I have enumeration declared in main.h header file and i wont to use it in files name.cpp ans name.h wich have definition and declaration of class name. And when i'm using enumeration in name.cpp everythink works fine but when i'm trying to use it in name.h it says unidentifed symbol

    main.h file
    Code:
    #ifndef _MAIN
    #define MAIN
    
    enum
    {
      Number=7,
      Unitcount=3
    };
    
    #endif
    name.cpp
    Code:
    #include<iostream.h>
    #include"main.h"
    #include"name.h"
    
    name::name()
    {
      for (int i=0;i<Unitcount;i++)
      {
         a[i]=Number;
      }
    }
    
    name::~name()
    {
    }
    name.h that works fine and now when i try to substitute 3 with Unitcount it says unidentified symbol 'Unitcount' and if i try to include main.h in here then it says multiple declaration
    Code:
    #ifndef _MODIFIERSET
    #define MODIFIERSET
    
    class name
    {
      public:
      name();
      ~name();
    
      private:
      int a[3];
    };
    
    #endif
    what's wrong or how to do it?

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    The header guards are supposed to match.
    Code:
    #ifndef _MODIFIERSET
    #define MODIFIERSET
    Otherwise, they aren't guards.

    And avoid leading underscores.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  3. #3
    Registered User
    Join Date
    Sep 2005
    Posts
    20
    Thanks.Hmm such an obvious mistake.

  4. #4
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Ireland
    Posts
    8,446
    include main.h in the name.h and remove it from name.cpp

    Also, ifndef and define preprocessor directive in main.h don't match. it's either _MAIN or MAIN.

    Also, use neither. _MAIN is a big nono. You shouldn't start any identifiers with the underscore symbol. They are reserved by the implementation. MAIN is a terrible name for guard headers. Use a sligtly more complex mechanism. One possible way can be:

    <YOUR_INITIALS>_<FILE_NAME>_<FILE_EXTENSION>_INC L

    So instead of MAIN you could have

    MJF_MAIN_H_INCL
    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  5. #5
    Reverse Engineer maxorator's Avatar
    Join Date
    Aug 2005
    Location
    Estonia
    Posts
    2,318
    Code:
    enum
    {
      Number=7,
      Unitcount=3
    };
    Should enum have a name?
    "The Internet treats censorship as damage and routes around it." - John Gilmore

  6. #6
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Ireland
    Posts
    8,446
    It's an hack.

    Number and Unitcount are equivalent to static consts with that name and those values. It seems old code had to do this. You couldn't define static variables or something similar inside classes (i'm not sure exactly what it was. Someone correct me on this if it's wrong). Nowadays it makes little sense. However if may be a convenient way to declare a bunch of sequential statics.
    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  7. #7
    Reverse Engineer maxorator's Avatar
    Join Date
    Aug 2005
    Location
    Estonia
    Posts
    2,318
    And how is that enum accessed?
    "The Internet treats censorship as damage and routes around it." - John Gilmore

  8. #8
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    Quote Originally Posted by maxorator
    And how is that enum accessed?
    Like Mario F. said, they behave like constant ints.

    You can store there value in an int like this:
    int num = Number;
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  9. #9
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Ireland
    Posts
    8,446
    just like any other const static var...

    myclass.identifier --> where identifier is one of the enum identifiers.

    Or if simply identifier if, just like with the OPs case, the enum belongs instead outside the scope of a class. (I just noticed the OP was declaring the enum on the global scope, no inside a class.)

    There is no rule that states an enumerator must be named. But on that case no variables of the type enumeration can be created. Instead the enum is of type static const int (although I suspect this is implementation defined).

    I have actually used for a short period unnamed enums inside namespaces as support for classes definitions. I since then switched over.
    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Memory problem with Borland C 3.1
    By AZ1699 in forum C Programming
    Replies: 16
    Last Post: 11-16-2007, 11:22 AM
  2. Someone having same problem with Code Block?
    By ofayto in forum C++ Programming
    Replies: 1
    Last Post: 07-12-2007, 08:38 AM
  3. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM
  4. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 06:54 PM
  5. beginner problem
    By The_Nymph in forum C Programming
    Replies: 4
    Last Post: 03-05-2002, 05:46 PM