
Originally Posted by
ankurjain
hi all,
suppose there are three files a.h,b.h,c.h
which contain classes a,b,c respectively.
and let c.h included in b.h and b.h included in a.h.
//c.h
class c
{ ....
};
//b.h
#include c.h
class c
{...
}
//a.h
#include b.h
#include c.h
class a
{...
}
//c.h
class c
{ ....
};
//b.h
#include c.h
class c
{...
}
//a.h
#include b.h
#include c.h
class a
{...
}
To copy to clipboard, switch view to plain text mode
On compiling there comes an error of class redeclaration ..... how can we prevent this ?
The standard way is
Generic header file
#ifndef <FILENAME>_H
#define <FILENAME>_H
class <ClassName>
{
};
#endif // <FILENAME>_H
#ifndef <FILENAME>_H
#define <FILENAME>_H
class <ClassName>
{
};
#endif // <FILENAME>_H
To copy to clipboard, switch view to plain text mode
In example
// a.h
#ifndef a_h
#define a_h
class a
{
};
#endif // a_h
// a.h
#ifndef a_h
#define a_h
class a
{
};
#endif // a_h
To copy to clipboard, switch view to plain text mode
// b.h
#ifndef b_h
#define b_h
#include "a.h"
class b
{
};
#endif // b_h
// b.h
#ifndef b_h
#define b_h
#include "a.h"
class b
{
};
#endif // b_h
To copy to clipboard, switch view to plain text mode
// c.h
#ifndef c_h
#define c_h
#include "a.h"
#include "b.h"
class c
{
};
#endif // c_h
// c.h
#ifndef c_h
#define c_h
#include "a.h"
#include "b.h"
class c
{
};
#endif // c_h
To copy to clipboard, switch view to plain text mode
In this way the second inclusion of "a.h" are ignored by precompiler.
Bookmarks