What's new

enumerations

|\/|-a-\/

Uli Hecht
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
 

Garstyciuks

New member
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:
OP
A

|\/|-a-\/

Uli Hecht
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:
OP
A

|\/|-a-\/

Uli Hecht
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...
 

Garstyciuks

New member
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)
 
OP
A

|\/|-a-\/

Uli Hecht
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?
 

Garstyciuks

New member
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.
 

Doomulation

?????????????????????????
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.
 
OP
A

|\/|-a-\/

Uli Hecht
yeah man, the third time now, i already noticed it :D...

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 ?!
 

Garstyciuks

New member
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.
 

Top