Archived

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

vbisme

Headers

Recommended Posts

I have a header like so: //Commons.h //////////////////// //Provides the commonly used macros, defines, and classes #ifndef __COMMONS_H__ #define __COMMONS_H__ #define SafeRelease(x) if (x != NULL) { x->Release(); } void OutputError(char *String) { OutputDebugString(String); OutputDebugString("\n"); } #endif Then I have two more modules: Mod1.h, Mod1.cpp, Mod2.h, and Mod2.cpp ///Mod1.h #include "Mod2.h" //the rest of the codes ///////// ///Mod1.cpp #include "Mod1.h" #include "Commons.h" //the rest of the code ///Mod2.h //codes ///Mod2.cpp #include "Mod2.h" #include "Commons.h" When compile I get redefinition errors. How to solve this?

Share this post


Link to post
Share on other sites
What''s being redefined?

If it''s SafeRelease, then is SafeRelease being defined in Mod1.h or Mod2.h?

If you''re getting something like "OutputError already defined in Mod2.cpp", then you need to define OutputError as static, or move its definition to its own cpp file.

All your bases belong to us

Share this post


Link to post
Share on other sites
The OutputError() function will be defined in every source-file/module that includes the commons.h header. Either just declare the function-prototype in the commons.h file and define the function in exactly one source file, or make the OutputError declaration inline or static. This will introduce some code bloat, but you will not get the redefinition errors (assuming that OutputError is the multiply defined symbol (and it should be, with the code you have currently)).

Share this post


Link to post
Share on other sites
Function-definitions should always be in cpp-files, will save you a lot of trouble att least

example:

stuff.h
#ifndef _STUFF_H_
#define _STUFF_H_
void DoStuff();
#endif


stuff.cpp
#include "stuff.h"
void DoStuff()
{
// code
}

Share this post


Link to post
Share on other sites