multiple definition

Discussion in 'C++' started by Samuele Armondi, Jun 30, 2003.

  1. "Jochen Zeischka" <> wrote in message
    news:bdrv8k$nki$...
    > Hi everybody!
    >
    > I have a question concerning code organisation. Suppose I have the

    following
    > header file:
    >
    > #ifndef SOME_NAME
    > #define SOME_NAME
    >
    > namespace N {
    > void F()
    >
    > ... here comes the implementation
    > }
    > }
    >
    > #endif
    >
    > In this case I would think that you never ever can encounter a 'multiple
    > definition' problem because
    > 1) SOME_NAME is defined the first time F is defined
    > 2) there is only one definition of F, so it even wouldn't be a problem

    to
    > define the same F a hundred times...
    >
    > Still, I get the error message that F is multiply defined when this header
    > file is used in other header files. (And there is definitely not another

    'F'
    > defined in any of these header files)
    >
    > Can anyone help me out?
    >
    > Thanks!
    >
    > Jochen
    >
    >

    I had the same problem. I solved it by splitting the code into two: the
    declarations in a .h file and _all_ the implementations in a .cpp file, i.e.
    // foo.h
    #ifndef foo_h
    #define foo_h

    namespace n
    {
    class bar
    {
    private:
    int i;
    public:
    bar(int);
    void f();
    };
    }
    #endif

    //foo.cpp
    n::bar::bar(int n) : i(n)
    {
    }

    void n::bar::f()
    {
    //whatever
    }

    HTH,
    S. Armondi
    Samuele Armondi, Jun 30, 2003
    #1
    1. Advertising

  2. Hi everybody!

    I have a question concerning code organisation. Suppose I have the following
    header file:

    #ifndef SOME_NAME
    #define SOME_NAME

    namespace N {
    void F()

    ... here comes the implementation
    }
    }

    #endif

    In this case I would think that you never ever can encounter a 'multiple
    definition' problem because
    1) SOME_NAME is defined the first time F is defined
    2) there is only one definition of F, so it even wouldn't be a problem to
    define the same F a hundred times...

    Still, I get the error message that F is multiply defined when this header
    file is used in other header files. (And there is definitely not another 'F'
    defined in any of these header files)

    Can anyone help me out?

    Thanks!

    Jochen
    Jochen Zeischka, Jul 1, 2003
    #2
    1. Advertising

  3. "Jochen Zeischka" <> wrote in message
    news:bdrv8k$nki$...
    > Hi everybody!
    >
    > I have a question concerning code organisation. Suppose I have the

    following
    > header file:
    >
    > #ifndef SOME_NAME
    > #define SOME_NAME
    >
    > namespace N {
    > void F()
    >
    > ... here comes the implementation
    > }
    > }
    >
    > #endif
    >
    > In this case I would think that you never ever can encounter a 'multiple
    > definition' problem because
    > 1) SOME_NAME is defined the first time F is defined
    > 2) there is only one definition of F, so it even wouldn't be a problem

    to
    > define the same F a hundred times...
    >
    > Still, I get the error message that F is multiply defined when this header
    > file is used in other header files. (And there is definitely not another

    'F'
    > defined in any of these header files)
    >
    > Can anyone help me out?
    >
    > Thanks!
    >
    > Jochen


    Please note that the header guards protect multiple inclusion of a header
    file in
    a single translation unit(e.g.cpp file). If you happen to include this
    header file in several
    translation units then the linker problem will arise. Either move the
    definition to some implementation file or
    else make the function inline(only if it suits in your case) .
    Josephine Schafer, Jul 1, 2003
    #3
    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. Jianli Shen
    Replies:
    1
    Views:
    557
    Victor Bazarov
    Mar 13, 2005
  2. Ark
    Replies:
    1
    Views:
    398
    Chris Torek
    Aug 7, 2004
  3. Jon Slaughter
    Replies:
    4
    Views:
    424
    Jon Slaughter
    Oct 26, 2005
  4. Replies:
    11
    Views:
    1,207
    Ian Collins
    Aug 5, 2006
  5. Pierre Yves
    Replies:
    2
    Views:
    468
    Pierre Yves
    Jan 10, 2008
Loading...

Share This Page