Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Re: Template and Multiple Definition

Reply
Thread Tools

Re: Template and Multiple Definition

 
 
Josephine Schafer
Guest
Posts: n/a
 
      07-31-2003

"LaBird" <(E-Mail Removed)> wrote in message
news:bgakji$b40$(E-Mail Removed)...
> Hi all,
>
> I have got a class file "class.h", defined like this:
>
> int globe = 0;
> template <typename T>
> class C {
> T x;
> public:
> void inc() {
> x++;
> globe++;
> }
> };
>
> and then a file for normal functions, "a.cpp":
>
> #include "class.h"
> void dec() {
> globe--;
> }
>
> Now I would like to write a main() in "main.cpp".
> However, if I compile with "g++ a.cpp main.cpp",
> the loader complains about "multiple definition
> of variable globe". I have got 2 questions: (1) Where
> should I put the definition and initialization of
> variable "globe", so that this loader error would
> not occur? (2) When I merge "class.h" and "a.cpp"
> together as a single .h file, the error disappears,
> but I find that globe is not initialized to 0 when
> I start running main(). What should I do?
>
> Thanks in advance.


Declare the global extern in the .h file and move the definition into the
implementation file a.cpp.
Remember there should be no two definitions of any global variable in your
program.


 
Reply With Quote
 
 
 
 
LaBird
Guest
Posts: n/a
 
      07-31-2003
Thanks!

"Josephine Schafer" <(E-Mail Removed)> wrote in message
news:bgalij$mvcji$(E-Mail Removed)-berlin.de...
>
> "LaBird" <(E-Mail Removed)> wrote in message
> news:bgakji$b40$(E-Mail Removed)...
> > Hi all,
> >
> > I have got a class file "class.h", defined like this:
> >
> > int globe = 0;
> > template <typename T>
> > class C {
> > T x;
> > public:
> > void inc() {
> > x++;
> > globe++;
> > }
> > };
> >
> > and then a file for normal functions, "a.cpp":
> >
> > #include "class.h"
> > void dec() {
> > globe--;
> > }
> >
> > Now I would like to write a main() in "main.cpp".
> > However, if I compile with "g++ a.cpp main.cpp",
> > the loader complains about "multiple definition
> > of variable globe". I have got 2 questions: (1) Where
> > should I put the definition and initialization of
> > variable "globe", so that this loader error would
> > not occur? (2) When I merge "class.h" and "a.cpp"
> > together as a single .h file, the error disappears,
> > but I find that globe is not initialized to 0 when
> > I start running main(). What should I do?
> >
> > Thanks in advance.

>
> Declare the global extern in the .h file and move the definition into the
> implementation file a.cpp.
> Remember there should be no two definitions of any global variable in your
> program.
>
>



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Run-time template list definition / Run-time variable type definition Pierre Yves C++ 2 01-10-2008 02:52 PM
Template member functions in template class with separate definition =?iso-8859-1?q?Erik_Wikstr=F6m?= C++ 5 12-14-2006 01:04 PM
Out-of-class definition of a non-template member of an explicitly specialized class template? BigMan C++ 3 04-22-2005 06:28 AM
fully specialized function template and "multiple definition" Hartmut Sbosny C++ 2 04-20-2005 04:51 PM
Re: Template and Multiple Definition ES Kim C++ 1 07-31-2003 09:22 AM



Advertisments