Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

Martee

Stupid Newbie Question - VC++

This topic is 6927 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

OK, I know people will laugh at me for this one, but here goes anyway: I have a project in VC++ 6.0. There are a number of .cpp files, and one main header file which contains all the global stuff. Each .cpp file includes that main header. The problem is, when i compile it, i get lots of errors on the second and third .cpp files, because everything in the header has already been defined in the first .cpp file. I hope I explained this well enough. How can I fix this??? Martee Magnum Games http://MagnumGames.8m.com

Share this post


Link to post
Share on other sites
Advertisement
Protect the header file like this

abc.h

#if !defined(_ABC_H)
#define _ABC_H

// put your code here


#endif

this prevents multiple includes

Share this post


Link to post
Share on other sites
I hate #ifdef s, so i would just include the main header in the main file, then include the cpps, and dont include the main header in the cpp''s.

Share this post


Link to post
Share on other sites
Since it''s MSVC, you can also do this:

#ifndef __YOURHEADERNAME_H__
#define __YOURHEADERNAME_H__

#if _MSC_VER >= 1000
#pragma once
#endif // _MSC_VER >= 1000

// classes here

#endif // !defined(__YOUHEADERNAME_H__)

that makes sure that the file is only included once in the project, on all compilers.

Share this post


Link to post
Share on other sites
I kindof agree with verminaard on this one - though under Visual Studio it''s almost impossible to do without the inclusion guards.

Ordering your includes in your various files so that you never duplicate is much cleaner, but it gets hard when you use things like MFC, because you never quite know what they include...


#pragma DWIM // Do What I Mean!
~ Mad Keith ~
**I use Software Mode**

Share this post


Link to post
Share on other sites
What does

#if _MSC_VER >= 1000
#pragma once
#endif // _MSC_VER >= 1000

do? Is _MSC_VER the version of the compiler?

/. Muzzafarath

Share this post


Link to post
Share on other sites
I don''t think there''s anything wrong with the #ifdef solution. Sure, it might seem like a hack, but when you start getting massive projects it becomes impossible to trace which needs what. I tend to believe that each CPP file should include all the headers it needs to compiler, and each header should contain all the other headers that are needed for that header to compiler, and that all headers should ensure that they are only included once. This makes managing them a lot easier.

Of course, reducing interdependencies between your headers is always a good idea. But you can only really take that so far.

Share this post


Link to post
Share on other sites
What''s to hate about #ifdefs?

Verminaard''s approach makes reusing any code in another project later nothing but a pain in the butt, because it''s all glommed together.

aig

Share this post


Link to post
Share on other sites
Ok, thanx everybody for the replies. I''ll be trying some of this stuff to see what works best.

Martee
Magnum Games
http://MagnumGames.8m.com

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!