Making classes and structs visible globally across a whole project
Hi,
I'm writing some code where some of the structs and classes I'm defining have to be visible to other files. For example, my Mesh class has to visible to my Utils.h/.cpp file because one of the functions takes in a Mesh object as an arguement (and obviously needs to know what a Mesh is).
However, I dunno where to define the Mesh object to make it visible and usable across all files. I tried to define it in Mesh.h and include Mesh.h in the other files that need to use it, but I kept getting compiler errors saying class redefinition.
Can someone tell me how they make their class and structure types usable across a whole project?
Cheers, Duncan
Looks like you need inclusion guards:
You should do it for every header file you create. Just take the name of the header, convert it to uppercase and use [smile]
Oxyd
//mesh.h:#ifndef MESH_H#define MESH_Hclass Mesh {...};#endif
You should do it for every header file you create. Just take the name of the header, convert it to uppercase and use [smile]
Oxyd
You need to do somethine like this in your Mesh.h file:
#ifndef _MESH_H_
#define _MESH_H_
// class definition goes here...
#endif
That should get rid of the class redefinition error.
#ifndef _MESH_H_
#define _MESH_H_
// class definition goes here...
#endif
That should get rid of the class redefinition error.
edit: oops! every body's posting at the same time? well here it is again-
If you declare mesh in a .h file, you'll want to put the preprocessor directives at the top and bottom of the file to prevent duplicate declarations.
a la:
What this does is:
checks to see if an arbitrary unique preprocessor label is defined (It shouldn't be for the first time). If it is not defined, then the compiler defines it and goes through your code. For subsequent #includes the compiler will see that the constant is already defined and will skip over your code.
If you declare mesh in a .h file, you'll want to put the preprocessor directives at the top and bottom of the file to prevent duplicate declarations.
a la:
#ifndef MY_MESH_HEADER#define MY_MESH_HEADER//your code here #endif
What this does is:
checks to see if an arbitrary unique preprocessor label is defined (It shouldn't be for the first time). If it is not defined, then the compiler defines it and goes through your code. For subsequent #includes the compiler will see that the constant is already defined and will skip over your code.
Cheers for the fast reply guys (means I won't have to remember what I was doing when I hit the bug for too long heh), #ifndef etc works a treat. Out of interest, how would I use this #pragma thing evolutional mentioned?
Read this carefully: Organising code files
As for #pragma once, just add it to the beginning of your header file. You should still leave the include guards in though, #pragma once has been known to be buggy.
As for #pragma once, just add it to the beginning of your header file. You should still leave the include guards in though, #pragma once has been known to be buggy.
Quote:Original post by Fruny
You should still leave the include guards in though, #pragma once has been known to be buggy.
Especially when you consider the versions of GCC that flag it as being depreciated.
Quote:Original post by Duncanf
Cheers for the fast reply guys (means I won't have to remember what I was doing when I hit the bug for too long heh), #ifndef etc works a treat. Out of interest, how would I use this #pragma thing evolutional mentioned?
I beleive like this:
//mesh.h#pragma onceclass Mesh { ...};
But the #pragma is compiler dependent (works on VC++ from certain version, IIRC). So I'd recommend you using the #ifndef thingy.
Oxyd
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement