directives

Started by
3 comments, last by Caesar 22 years, 9 months ago
Hi, I created a project and added a lot of header and source pairs. And then I''ve needed to include each header in each other header (so that I can use code from each file in each other file). But an problem occured, a "Already defined in xxx.obj" apeared. How can I solve this problem? All the header are locked (#ifndef #define #endif), the extern prefix is also useless (or at least in the way I use it). Any other notes to this topic also welcomed Many thanks
Advertisement
Well, you can''t have any objects declared in headers, otherwise you''ll get that error. In other words, you can''t have something like int a in anything but an implementation file (.c, .cc, .cpp, etc.). As for functions, the same applies, unless they''re declared as inline.
Yeah, but header files are used to declare this, I need to use, for example, variable X, declared in a.h (or whatever extension the file will have) and use it in another 10 files, I cannot imagine how to do it this way
declare a variable as extern in the headers and then define it in ONE .c/.cpp file for the variable to be useable across modules.

ex.

in foobar.h:
      extern int foobar;  


in foobar.cpp:
  int foobar = 0xFF; // whatever value you want, or no value at all.  

Just remember, that if you want the variable to have an initial value, only supply it in the DEFINITION (i.e. in the .c/.cpp file), never in the extern declaration (in the .h file).

Now you can include the .h file in as many modules as you want, and it shouldn't cause any problems.

Edited by - Dactylos on July 29, 2001 6:58:30 PM
As for variables, this fits, but what to do when it comes to classes, function etc?

sthing.h
  class CSomething{public:   int Hello();};extern CSomething something;   


sthing.cpp
  int CSomething::Hello(){   return 0;}   


thanks


Edited by - Caesar on July 30, 2001 7:09:34 AM

This topic is closed to new replies.

Advertisement