Results 1 to 10 of 10

Thread: enumerations

  1. #1
    Uli Hecht
    Join Date
    Mar 2005
    Location
    Schwabach (Germany)
    Posts
    71

    Question enumerations

    Hi,



    i don't know why the hell i've problems with enumerations... i've got them never before...

    Whats's wrong with my code?:

    main.c
    Code:
    #include "main.h"
    
    int main() {
       return 0;
    }
    
    STRUCT_A a[] = {
       {ENUM_A_1, ENUM_A_2, 5},
       {ENUM_A_2, ENUM_A_1, 3},
    };
    main.h
    Code:
    #if !defined(MAIN_H)
    #define MAIN_H
    
    int main();
    
    enum ENUM_A {
       ENUM_A_1,
       ENUM_A_2,
    };
    
    struct STRUCT_A {
       ENUM_A e_a;
       ENUM_A e_b;
       int    c;
    };
    
    extern STRUCT_A a[];
    
    #endif
    uli
    MS WORD: "Warning! Hyperlinks can damage your computer and data. [...]"


    • Advertising

      advertising
      EmuTalk.net
      has no influence
      on the ads that
      are displayed
        
       

  2. #2
    EmuTalk Member Garstyciuks's Avatar
    Join Date
    Jan 2006
    Posts
    152
    You can't define "a" two times. You include main.h into main.c, it already defines a as extern and then you redefine it. Remove the line extern STRUCT_A a[]; And I don't quite see the use of defining int main(); in the main.h, since main is defined in main.c and it is probably going to be called only once. Alternate solution might be to restructure main.h file as following:
    Code:
    enum ENUM_A {
       ENUM_A_1,
       ENUM_A_2,
    };
    
    struct STRUCT_A {
       ENUM_A e_a;
       ENUM_A e_b;
       int    c;
    };
    
    #if !defined(MAIN_H)
    #define MAIN_H
    
    int main();
    
    extern STRUCT_A a[];
    
    #endif
    and define MAIN_H in main.c before including main.h.
    Last edited by Garstyciuks; February 20th, 2006 at 14:33.

  3. #3
    Uli Hecht
    Join Date
    Mar 2005
    Location
    Schwabach (Germany)
    Posts
    71
    yes, it's extern, so it must be defined anywhere...
    i tried other things and slowly become confused:

    Code:
    class A {
       public:
          int Val1;
          int Val2;
    };
    
    int main() {
       return 0;
    }
    compiler:
    Code:
    Compiling...
    main.c(14) : error C2061: syntax error : identifier 'A'
    main.c(14) : error C2059: syntax error : ';'
    main.c(14) : error C2449: found '{' at file scope (missing function header?)
    main.c(18) : error C2059: syntax error : '}'
    (i'm showing the complete main.c file)
    Last edited by |\/|-a-\/; February 20th, 2006 at 14:50.
    MS WORD: "Warning! Hyperlinks can damage your computer and data. [...]"

  4. #4
    Uli Hecht
    Join Date
    Mar 2005
    Location
    Schwabach (Germany)
    Posts
    71
    ahhhh.... it's called main.c and i'm programming c++... renamed the file and it works fine, maybe it's the same with the enums...
    MS WORD: "Warning! Hyperlinks can damage your computer and data. [...]"

  5. #5
    EmuTalk Member Garstyciuks's Avatar
    Join Date
    Jan 2006
    Posts
    152
    The only thing I see wrong with your enums is that you defined 'a' two times. Your code does compile on my compilator (VC++7.1)

  6. #6
    Uli Hecht
    Join Date
    Mar 2005
    Location
    Schwabach (Germany)
    Posts
    71
    thanks but i already noticed that u can't use a .c file with cpp code in VC6...

    how do you declare variables, if you have more files, and each file should have access to it?
    MS WORD: "Warning! Hyperlinks can damage your computer and data. [...]"

  7. #7
    EmuTalk Member Garstyciuks's Avatar
    Join Date
    Jan 2006
    Posts
    152
    It's just that you don't have to define a variable more than one time per a file. One main file should have a normal definition, and other should have extern definition. This example would be wrong: in main.cpp:
    Code:
    #include "main.h"
    int asdf;
    and in main.h:
    Code:
    extern int asdf;
    That kind of main.h could be used for other files, but not for main.cpp. To make it useable, you could do this at main.cpp:
    Code:
    #define MAIN_H
    #include "main.h"
    int asdf;
    and this for main.h:
    Code:
    #ifndef MAIN_H
    #define MAIN_H
    extern int asdf;
    //any other data that mustn't be redeclared
    #endif
    //any data types definition, classes, etc...
    I hope this helps you.

  8. #8
    ????????????????????????? Doomulation's Avatar
    Join Date
    Nov 2001
    Location
    ????????????????
    Posts
    8,780
    It does indeed work to declare a variable declared in say main.cpp, declared as extern in main.h. The compiler would think the variable is extern and therefore ignore it. The linker will still find it and therefore it will compile fine.

    This is a common method I use when I want multiple files to access stuff in a .cpp file that should be public.

    Also, there is a differenece between C and C++. C does not support classes, for example. If you name your file with a .c extension, the compiler will by default compile it as C code (not C++). This can be overriden in project settings.
    Atashi wa juu-yon-sai no onna no ko! Atashi no namae wa Miizuki. Yurushiku ne!
    Nani? Atashi o shinjirimasen desu ka? Baka!
    "You're all doomed! Doomed, I say! Hehe... are we approaching the end of the world?"

    shikata ga kaite aru - "the instructions are written above"
    Need to download GoodN64 or instructions to use it? Need to check if it's a good or bad rom?
    Download: Glide64 | Hacktarux's wrapper

  9. #9
    Uli Hecht
    Join Date
    Mar 2005
    Location
    Schwabach (Germany)
    Posts
    71
    yeah man, the third time now, i already noticed it ...

    well, thanks for helping, but that was actually what i did in my first entry of this thread... Garstyciuks, u had told me, that i can't define a two times, and later your're doing the same and tell me, it is right ?!
    MS WORD: "Warning! Hyperlinks can damage your computer and data. [...]"

  10. #10
    EmuTalk Member Garstyciuks's Avatar
    Join Date
    Jan 2006
    Posts
    152
    If you would look closer, MAIN_H is defined before including main.h in main.cpp, so it skips the part where extern int asdf is defined.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •