Sign in to follow this  

Making classes and structs visible globally across a whole project

This topic is 4856 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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

Share this post


Link to post
Share on other sites
Looks like you need inclusion guards:


//mesh.h:

#ifndef MESH_H
#define MESH_H

class 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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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:

#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.

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
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 once

class Mesh {
...
};


But the #pragma is compiler dependent (works on VC++ from certain version, IIRC). So I'd recommend you using the #ifndef thingy.

Oxyd

Share this post


Link to post
Share on other sites
Just like to add on top of inclusion guards & pragmas, i recommend getting into the habbit of avoiding to include headers in headers as much as you possiably can!!!, only wont declarations in headers not any definition what so ever (except for inline functions of course). extern keyword, forward class declarations & reference/pointer types will help you to achieve this, even the standard library trys to help with this I/O streams lib have a header with just forward class declarations when you only need the name of a type its called iosfwd.

Share this post


Link to post
Share on other sites
Well - once I thought about a header that would behave differently depending on wheter a macro is defined or not. I mean:


//header.h
#ifndef HEADER_H_FWD

#ifndef HEADER_H
#define HEADER_H

class Header {
...
};

#endif

#else

class Header;

#endif



And then if you defined HEADER_H_FWD before #including it, it would only include the declaration of the object.

I've never included system like this, because I think, it looks ugly and so on... But I throw it here for discussion [smile]

Oxyd

Share this post


Link to post
Share on other sites

This topic is 4856 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this