Re: Template and Multiple Definition

Discussion in 'C++' started by ES Kim, Jul 31, 2003.

  1. ES Kim

    ES Kim Guest

    "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.
    >
    > Regards,
    > LaBird (Benny).
    >
    >


    (1) I can't tell for sure without main.cpp, but you might have included
    class.h in main.cpp. If it is the case, you defined globe twice, which
    is the cause of the error. Replace "int globe = 0;" with "extern int globe;"
    and add "int globe = 0;" into a.cpp or main.cpp.
    extern int globe; tells the compiler "there is an int variable named globe
    somewhere." int globe = 0; means "make an int variable named globe with
    initial value of 0".

    (2) You copied all the contents of a.cpp into class.h and threw away a.cpp,
    right? (and included class.h in main.cpp, of course.) Hmm, there shouldn't
    be any problem in this case. What have you done exactly?

    --
    ES Kim
     
    ES Kim, Jul 31, 2003
    #1
    1. Advertisements

  2. ES Kim

    LaBird Guest

    Maybe I've made a mistake somewhere in my compilation.
    Now it works alright. Thanks.

    Regards,
    LaBird (Benny).


    "ES Kim" <> wrote in message
    news:bgajsn$6rh$...
    > (2) You copied all the contents of a.cpp into class.h and threw away

    a.cpp,
    > right? (and included class.h in main.cpp, of course.) Hmm, there shouldn't
    > be any problem in this case. What have you done exactly?
    >
    > --
    > ES Kim
    >
    >
     
    LaBird, Jul 31, 2003
    #2
    1. Advertisements

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. Josephine Schafer

    Re: Template and Multiple Definition

    Josephine Schafer, Jul 31, 2003, in forum: C++
    Replies:
    1
    Views:
    2,192
    LaBird
    Jul 31, 2003
  2. Georg Teichtmeister

    Template linking problem (multiple definition)

    Georg Teichtmeister, Sep 5, 2003, in forum: C++
    Replies:
    11
    Views:
    797
    Geoff Macartney
    Sep 6, 2003
  3. Hartmut Sbosny
    Replies:
    2
    Views:
    844
    Hartmut Sbosny
    Apr 20, 2005
  4. BigMan
    Replies:
    3
    Views:
    1,592
    BigMan
    Apr 22, 2005
  5. =?iso-8859-1?q?Erik_Wikstr=F6m?=
    Replies:
    5
    Views:
    2,606
    =?iso-8859-1?q?Erik_Wikstr=F6m?=
    Dec 14, 2006
  6. Hendrik Schober
    Replies:
    16
    Views:
    707
    Hendrik Schober
    Sep 4, 2007
  7. Pierre Yves
    Replies:
    2
    Views:
    718
    Pierre Yves
    Jan 10, 2008
  8. Hel
    Replies:
    1
    Views:
    446
    Noah Roberts
    May 30, 2008
Loading...