Thread: how to declare an external variable of type "enum"?

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User
    Join Date
    Jun 2011
    Posts
    78

    Unhappy how to declare an external variable of type "enum"?

    If I have a global integer x, which I declare in main.c and want to use in function.c, I am able to do so by stating the following in header.h:

    Code:
    extern int x;
    However, what do I do if I have a global enum? In my main.c I declare a global variable as follows:

    Code:
    enum whoseturn {B, W};
    enum whoseturn color=W;
    But how do I declare it in my header in order to use it in function.c? I have tried such things as

    Code:
    extern enum whoseturn color;
    extern whoseturn color;
    extern enum color;
    But I consistently get error messages when I try to set color to B or W, to the effect B and W have not been declared. How do I do this globally?
    Last edited by Adam Rinkleff; 06-22-2011 at 07:35 PM.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    The definition of the enum type must be visible when compiling (and remember: source code files are compiled independently of one another). This is the job header files were born to do. You can put the enum definition and the extern in the header, and any source code that needs to know about it #includes the header.

  3. #3
    Registered User
    Join Date
    Jun 2011
    Posts
    78
    Quote Originally Posted by tabstop View Post
    The definition of the enum type must be visible when compiling (and remember: source code files are compiled independently of one another). This is the job header files were born to do. You can put the enum definition and the extern in the header, and any source code that needs to know about it #includes the header.
    : O That is the correct answer, two points for tabstop. I will simply place in header.h the code that is currently in main.c

    Code:
    enum whoseturn {B, W};
    enum whoseturn color=W;

  4. #4
    Registered User
    Join Date
    Jun 2011
    Posts
    78
    Oh, no, that isn't working quite right after all. I added it to the header, but now I'm getting this error:

    Code:
    input.o(.data+0x0): multiple definition of `color'
    main.o(.data+0x0): first defined here
    convertfen.o(.data+0x0): multiple definition of `color'
    main.o(.data+0x0): first defined here
    I don't define it in main anymore, so it seems to be defining it everytime the header is accessed?

  5. #5
    Registered User
    Join Date
    Jun 2011
    Posts
    78
    This is in my header file:
    Code:
    enum whoseturn color {B, W};
    This is in my main.c:
    Code:
    color=W;
    This is my error:
    Code:
    main.c: In function `main':
    main.c:62: `color' undeclared (first use in this function)
    main.c:62: `W' undeclared (first use in this function)

  6. #6
    Registered User
    Join Date
    Jun 2011
    Posts
    78
    And this is what I need in my header:
    Code:
    enum whoseturn {B, W};
    enum whoseturn color;
    Thanks everyone!

  7. #7
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by Adam Rinkleff View Post
    And this is what I need in my header:
    Code:
    enum whoseturn {B, W};
    enum whoseturn color;
    Thanks everyone!
    No that most definitely isn't what you need in your header. You need the definition of the whoseturn, and an extern variable declaration. Right now you have two separate color variables, one in your main, and one in your other file.

  8. #8
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    No, that won't work. The definition of the enum type refers to only the first of those lines. The second is a definition of a variable of that type. That stays in your c file.
    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
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    Nope.
    Code:
    enum whoseturn {B, W};
    Goes in the header.

    Code:
    enum whoseturn color;
    or
    Code:
    enum whoseturn color=W;
    go in your .c file.

    EDIT:
    Actually it should be:
    Code:
    enum color{B, W};
    Goes in the header.

    Code:
    enum color whoseturn ;
    or
    Code:
    enum color whoseturn=W;
    Sinse logically, the enum is of two colors, whereas the variable tells who's turn it is.


    I disagree with tabstop that an extern variable is appropriate. You don't need a global variable for something like this.
    Last edited by King Mir; 06-22-2011 at 07:59 PM.
    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.

  10. #10
    Registered User
    Join Date
    Jun 2011
    Posts
    78
    Quote Originally Posted by King Mir View Post
    You don't need a global variable for something like this.
    I do because I don't really know what I'm doing, so I'm happy if it compiles, and passing variables back and forth seems awfully difficulty.

  11. #11
    Registered User
    Join Date
    Jun 2011
    Posts
    78
    : DDDDDDd

  12. #12
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Adam Rinkleff
    I do because I don't really know what I'm doing, so I'm happy if it compiles, and passing variables back and forth seems awfully difficulty.
    Working with global variables is even more difficult because you have to keep global state in your mind all the time. When you don't really know what you're doing, global variables => guaranteed epic fail.
    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

  13. #13
    Registered User
    Join Date
    Jun 2011
    Posts
    78
    Quote Originally Posted by laserlight View Post
    Working with global variables is even more difficult because you have to keep global state in your mind all the time. When you don't really know what you're doing, global variables => guaranteed epic fail.
    Can you explain, because I really don't understand. I don't really have a global state to keep track of, I think you are overestimating the amount of skill that I possess. I'm just trying to write a chess program (I'm a chess player, not a programmer), and I find that just about every function I make wants to know the color, along with about 50'000 other variables, so I've just made everything global.

  14. #14
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,949
    Quote Originally Posted by Adam Rinkleff View Post
    ...I think you are overestimating the amount of skill that I possess. I'm just trying to write a chess program (I'm a chess player, not a programmer), ...
    You do realize that creating a chess program is one of the more difficult programming tasks to perform right?

  15. #15
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    Quote Originally Posted by AndrewHunter View Post
    You do realize that creating a chess program is one of the more difficult programming tasks to perform right?
    Not true, creating a good chess program might be very hard; but the bad ones was not so hard.

    Tim S.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. GCC lets you forward declare a struct with "class /name/"
    By Mozza314 in forum C++ Programming
    Replies: 12
    Last Post: 02-20-2011, 04:52 AM
  2. Replies: 3
    Last Post: 05-01-2010, 02:26 AM
  3. problem about handle "double" variable type? or?
    By Mathsniper in forum C Programming
    Replies: 4
    Last Post: 12-31-2006, 10:11 PM
  4. "Deciding" in runtime the type of a variable
    By mikahell in forum C++ Programming
    Replies: 28
    Last Post: 07-22-2006, 09:51 AM
  5. Ask about error when declare "static inline int"
    By ooosawaddee3 in forum C Programming
    Replies: 3
    Last Post: 05-25-2002, 05:03 AM