class declaration and definition inside a cpp file

Discussion in 'C++' started by Belebele, Oct 17, 2006.

  1. Belebele

    Belebele Guest

    In a cpp file, say a.cpp I have the following code:

    #include <element.h>

    struct Functor
    {
    void operator (Element const& ) const { /* ... */ }
    };

    /* ... */
    collOfElements.forEachElement(Functor());

    Then, on another cpp file, b.cpp I have

    #include <element.h>

    struct Functor
    {
    void operator (Element const& ) const { /* different
    from the functor in a.cpp */ }
    };

    /* ... */
    anotherCollOfElements.forEachElement(Functor());


    The code that the linker produces calls Functor::eek:perator() defined in
    a.cpp when the anotherCollOfElements (defined in b.cpp) is iterated
    over. My intention is that the Functor structs, although they have the
    same name, are different since they reside in different files and each
    functor is called when the code in its cpp file invokes it.

    Moreover, if I define b.cpp's Functor::eek:perator() outside of the class,
    the intended calls are made.

    I am confused as to what the linker is doing. Any idea?

    Thanks
     
    Belebele, Oct 17, 2006
    #1
    1. Advertising

  2. * Belebele:
    > In a cpp file, say a.cpp I have the following code:
    >
    > #include <element.h>
    >
    > struct Functor
    > {
    > void operator (Element const& ) const { /* ... */ }
    > };


    No, you don't have that code.

    Have you tried compiling?

    Please don't type in arbitrary pseudo-code when posting: copy and paste
    real code (do you understand why?).


    > /* ... */
    > collOfElements.forEachElement(Functor());
    >
    > Then, on another cpp file, b.cpp I have
    >
    > #include <element.h>
    >
    > struct Functor
    > {
    > void operator (Element const& ) const { /* different
    > from the functor in a.cpp */ }
    > };
    >
    > /* ... */
    > anotherCollOfElements.forEachElement(Functor());
    >
    >
    > The code that the linker produces calls Functor::eek:perator() defined in
    > a.cpp when the anotherCollOfElements (defined in b.cpp) is iterated
    > over.


    You have violated the One Definition Rule (the ODR): you can't have two
    classes with the same name in the same namespace, here the global one.

    Put the classes in different namespaces.

    E.g. anonymous namespaces.


    --
    A: Because it messes up the order in which people normally read text.
    Q: Why is it such a bad thing?
    A: Top-posting.
    Q: What is the most annoying thing on usenet and in e-mail?
     
    Alf P. Steinbach, Oct 17, 2006
    #2
    1. Advertising

  3. Belebele

    Martin Steen Guest

    Belebele wrote:

    > I am confused as to what the linker is doing. Any idea?


    Yes.

    1. use different names for different functions
    2. put the declarations in header files

    So you and the linker won't get confused ;)

    -Martin
     
    Martin Steen, Oct 17, 2006
    #3
  4. Belebele

    Belebele Guest


    > 1. use different names for different functions


    So, no link-time support to indicate a class name collision? Have to
    wait until run-time, when the app crashes to realize the little slip
    (or worse, it may not crash at all, and produce undefined behavior)

    > 2. put the declarations in header files


    Seems excessive for classes only used in one cpp module to increase
    encapsulation, to avoid duplication, to make the code easier to
    understand (all that good stuff, you know ...)
     
    Belebele, Oct 17, 2006
    #4
  5. Belebele

    Gavin Deane Guest

    Belebele wrote:
    > > 1. use different names for different functions

    >
    > So, no link-time support to indicate a class name collision? Have to
    > wait until run-time, when the app crashes to realize the little slip
    > (or worse, it may not crash at all, and produce undefined behavior)


    What support your linker might offer you is outside the scope of the
    C++ language. Violating the one definition rule is undefined behaviour
    as far as C++ is concerned.

    > > 2. put the declarations in header files

    >
    > Seems excessive for classes only used in one cpp module to increase
    > encapsulation, to avoid duplication, to make the code easier to
    > understand (all that good stuff, you know ...)


    Have you looked at whether anonymous namespaces will solve your
    problem?

    Gavin Deane
     
    Gavin Deane, Oct 17, 2006
    #5
    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. DrUg13
    Replies:
    1
    Views:
    490
    DrUg13
    Feb 10, 2004
  2. Jianli Shen
    Replies:
    1
    Views:
    621
    Victor Bazarov
    Mar 13, 2005
  3. Vinu
    Replies:
    9
    Views:
    663
  4. Replies:
    1
    Views:
    408
    Wang Dong
    Jan 16, 2007
  5. =?ISO-8859-2?Q?Miros=B3aw?= Makowiecki

    Decralation of class inside other class and definition outside this class

    =?ISO-8859-2?Q?Miros=B3aw?= Makowiecki, Jul 13, 2007, in forum: C++
    Replies:
    2
    Views:
    351
    Alf P. Steinbach
    Jul 13, 2007
Loading...

Share This Page