Re: Template and Multiple Definition

Discussion in 'C++' started by Josephine Schafer, Jul 31, 2003.

  1. "LaBird" <> wrote in message
    news:bgakji$b40$...
    > 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.
     
    Josephine Schafer, Jul 31, 2003
    #1
    1. Advertising

  2. Josephine Schafer

    LaBird Guest

    Thanks!

    "Josephine Schafer" <> wrote in message
    news:bgalij$mvcji$-berlin.de...
    >
    > "LaBird" <> wrote in message
    > news:bgakji$b40$...
    > > 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.
    >
    >
     
    LaBird, Jul 31, 2003
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. ES Kim
    Replies:
    1
    Views:
    336
    LaBird
    Jul 31, 2003
  2. Hartmut Sbosny
    Replies:
    2
    Views:
    747
    Hartmut Sbosny
    Apr 20, 2005
  3. BigMan
    Replies:
    3
    Views:
    1,436
    BigMan
    Apr 22, 2005
  4. =?iso-8859-1?q?Erik_Wikstr=F6m?=
    Replies:
    5
    Views:
    2,328
    =?iso-8859-1?q?Erik_Wikstr=F6m?=
    Dec 14, 2006
  5. Pierre Yves
    Replies:
    2
    Views:
    489
    Pierre Yves
    Jan 10, 2008
Loading...

Share This Page