Mail Archives: djgpp/1997/02/08/14:55:58
On Sat, 08 Feb 1997 09:31:47 -0500, SoMeOnE <bradh AT praxis DOT net> wrote:
>Thanks for reading this message....my problem is when I try to make and
>include file ( .h ) and include it in multiple C++ files...it comes up
>with an error on all int's and char's that they have already been
>defined....
You have to put the variable definition into exactly one .cc file, and
'extern' the declaration in the .h file.
e.g.
--- file1.cc ---
#include "file1.h"
#include "file2.h"
char something;
int main (void)
{
something=1;
printit();
something=2;
printit();
return 0;
}
--- file2.cc ---
#include <stdio.h>
#include "file1.h"
void printit()
{
printf("%d\n",something);
}
--- file1.h ---
extern char something;
--- file2.h ---
void printit();
--- end ---
Note that the header file says 'extern ...' and the .cc file just has
the definition.
George Foot
- Raw text -