Stupid Newbie Question - VC++
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
Protect the header file like this
abc.h
#if !defined(_ABC_H)
#define _ABC_H
// put your code here
#endif
this prevents multiple includes
abc.h
#if !defined(_ABC_H)
#define _ABC_H
// put your code here
#endif
this prevents multiple includes
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.
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.
#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.
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**
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**
What does
#if _MSC_VER >= 1000
#pragma once
#endif // _MSC_VER >= 1000
do? Is _MSC_VER the version of the compiler?
/. Muzzafarath
#if _MSC_VER >= 1000
#pragma once
#endif // _MSC_VER >= 1000
do? Is _MSC_VER the version of the compiler?
/. Muzzafarath
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.
Of course, reducing interdependencies between your headers is always a good idea. But you can only really take that so far.
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
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
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement