Advertisement

define a class...?

Started by September 14, 2001 02:48 PM
3 comments, last by _Titan_ 23 years, 5 months ago
I have two files, global vars and global inc these are header files, i have a class defined in global vars, how would i define that class in my other header file? I hope thi smakes sense. I am trying to use extern cCharactor Charactor; and cCharactor is in my global vas header and this code will be in the global inc header, how owuld i define the class so i can use it in my global inc header?
Have you included the necessary header file within your header file?

Try putting this in global_inc.h:

#include "global_vars.h"

If it''s in another folder you must specify the path.

To keep from getting compiler errors you should use some macros in your header files like this:

#ifndef GLOBAL_INC_H
#define GLOBAL_INC_H

(this is where your header stuff goes)
#endif

Do likewise for your other header. Note that this might be specific to Visual Studio, which is what I use, so if you''re using something else you might want to make sure that the compiler commands are correct.

Hope this helps...

eriol
Advertisement
This should help.

FIRST:
In your global vars header and global inc header, make the following your first two lines of code:

#ifndef GLOBAL_VARS_H // do GLOBAL_INC_H for the other file
#define GLOBAL_VARS_H // I made it a habit to do this in all my
// headers.

The very last line of each file should then be:

#endif

This will make sure that the header is only included once for each object at compile-time (avoid multiple declaration erros).

SECOND:
Say you defined class "foo" in Globalvars.h and want to use it in Globalinc.h.

Add the line #include "Globalvars.h" to your Globalinc.h file, and you can now use that class.

If you wrote classes in both headers and want to use both classes in each header, its okay. The #ifndef and #endif commands tell the compiler not to infinitely read the header files.

If the compiler is still not happy (I can''t remember the exact error), you can make the compiler comlply by declaring foo (back to the example) in Globalinc.h header without redefining it by adding:

class foo;

This will give the compiler a "heads up" that will allow you to use "foo" within the header as a type.

Hope this makes sense and helps you out.
Well, i have put the # directives in my header files and if i just added the global vars header to my globalinc header, i woudlnt need the global inc file at all! I''d just use the globaql vas header...Well, I will see what i can do with this info, i''ll post about it tommorow. Thnx for your help, any more suggestions are welcome.
Well, i added the #ifndef #defien and #endif directives to all my header files, but i still get errors of multiple declaractions from the linker I get the linker errors for every obj file thats created in my project...now what?

NOTE: I added the /FORCE:MULTIPLE to my project settings, and it will compile an link now, but i still get a mass load of warnings, i want these to go away.

I only have one header now for global variables and functions definitions, so every .cpp file calls this header. This header has the

#ifndef ENGINE_GLOBAL_VARS_H
#define ENGINE_GLOBAL_VARS_H

...

#endif

Edited by - _titan_ on September 15, 2001 1:57:48 PM

This topic is closed to new replies.

Advertisement