ewe@gator.cacs.usl.edu (Edwin Wallace Elberson) (03/25/90)
I've got it straightened out now; I guess the problem was with my concept of definitions and declarations. However, after seeing what I was trying to do, does anyone have any tips on including the same header in files that are to be compiled at the same time? Being new to C++, I'd like to see what other people do in this situation, and how their techniques differ from the way I'm doing it now. Thanks, ewe@gator.cacs.usl.edu
ark@alice.UUCP (Andrew Koenig) (03/26/90)
In article <5790@rouge.usl.edu>, ewe@gator.cacs.usl.edu (Edwin Wallace Elberson) writes: > I've got it straightened out now; I guess the problem was with my concept > of definitions and declarations. However, after seeing what I was trying to > do, does anyone have any tips on including the same header in files that > are to be compiled at the same time? Being new to C++, I'd like to see what > other people do in this situation, and how their techniques differ from > the way I'm doing it now. Thanks, The short answer is: Suppose you have a class Foo that you want to use in several separately compiled parts of your program. Create a file called Foo.h that looks like this: #ifndef Foo_flag #define Foo_flag 1 other stuff goes here #endif where `other stuff' is the class declaration itself and declarations of inline functions relevant to class Foo. Everything else that's part of class Foo goes in a separate file called Foo.c that begins this way: #include "Foo.h" and then contains definitions of member functions that aren't inline, definitions of class statics, and so on. Now you can include Foo.h in every source file that needs it without worrying about duplicate definitions, etc. -- --Andrew Koenig ark@europa.att.com
rfg@ics.uci.edu (Ronald Guilmette) (03/29/90)
In article <10625@alice.UUCP> ark@alice.UUCP (Andrew Koenig) writes: >In article <5790@rouge.usl.edu>, ewe@gator.cacs.usl.edu (Edwin Wallace Elberson) writes: > >> I've got it straightened out now; I guess the problem was with my concept >> of definitions and declarations. However, after seeing what I was trying to >> do, does anyone have any tips on including the same header in files that >> are to be compiled at the same time? Being new to C++, I'd like to see what >> other people do in this situation, and how their techniques differ from >> the way I'm doing it now. Thanks, > >The short answer is: > > Suppose you have a class Foo that you want to use in > several separately compiled parts of your program. > > Create a file called Foo.h that looks like this: > > #ifndef Foo_flag > #define Foo_flag 1 > > other stuff goes here > > #endif Or, if you happen to be using the GNU g++ compiler (with the GNU preprocessor), and you are not too worried about ANSI-C compatibility, you can use a little feature that I invented (and implemented), to wit: #pragma once When this appears in an include file, the preprocessor will not include that particular file more than once during the current compilation. // Ron Guilmette (rfg@ics.uci.edu) // C++ Entomologist // Motto: If it sticks, force it. If it breaks, it needed replacing anyway.