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. Advertising

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

    Re: Template and Multiple Definition

    Josephine Schafer, Jul 31, 2003, in forum: C++
    Replies:
    1
    Views:
    2,113
    LaBird
    Jul 31, 2003
  2. Hartmut Sbosny
    Replies:
    2
    Views:
    738
    Hartmut Sbosny
    Apr 20, 2005
  3. BigMan
    Replies:
    3
    Views:
    1,418
    BigMan
    Apr 22, 2005
  4. =?iso-8859-1?q?Erik_Wikstr=F6m?=
    Replies:
    5
    Views:
    2,303
    =?iso-8859-1?q?Erik_Wikstr=F6m?=
    Dec 14, 2006
  5. Pierre Yves
    Replies:
    2
    Views:
    473
    Pierre Yves
    Jan 10, 2008
Loading...

Share This Page