Returning a const reference brokes dependency among header files?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Kermit Mei

    Returning a const reference brokes dependency among header files?

    Hello all, I wrote four simple c++ source files for this test. Look
    please:
    /*************1* **************/
    /// file A.h
    #ifndef A_H
    #define A_H

    class A {
    public:
    void set(int i);
    const int & get();
    private:
    int a;
    };

    #endif

    /**************2 *************** */
    ///file A.cpp
    #include "A.h"


    inline void A::set(int i) {
    a = i;
    }

    inline const int & A::get() {
    return a;
    }


    /*************** **3************ *******/
    ///fileB.h
    #ifndef B_H
    #define B_H
    #include "A.h"

    class A;

    class B
    {
    public:
    B();

    const int & getB();
    private:
    A ab;
    };

    #endif

    /*************** ****4********** ******/
    /// file B.cpp
    #include "B.h"

    B::B()
    { ab.set(19); }

    const int & B::getB()
    { return ab.get(); }


    /*************** ****main.cpp*** ******/
    #include "B.h"
    #include <iostream>

    using namespace std;

    int main()
    {
    B ab;
    cout << ab.getB() << endl;
    }

    /*************** *************** *******/
    Now, compile them and the errors occurs:
    c++ -Wall -o main main.cpp A.cpp B.cpp
    /tmp/ccB4KNX2.o: In function `B::getB()':
    B.cpp:(.text+0x d): undefined reference to `A::get()'
    /tmp/ccB4KNX2.o: In function `B::B()':
    B.cpp:(.text+0x 29): undefined reference to `A::set(int)'
    /tmp/ccB4KNX2.o: In function `B::B()':
    B.cpp:(.text+0x 45): undefined reference to `A::set(int)'
    collect2: ld returned 1 exit status
    >
    It's funny that if you merge them into one head file, then
    you can run it:

    /************* ab.h ************/
    #ifndef T_H
    #define T_H

    class A {
    public:
    void set(int i);
    const int & get();
    private:
    int a;
    };

    class B
    {
    public:
    B();
    const int & getB();
    private:
    A ab;
    };

    #endif


    /*********** ab.cpp *************** **/
    #include "t.h"

    inline void A::set(int i) {
    a = i;
    }

    inline const int & A::get() {
    return a;
    }

    B::B()
    { ab.set(19); }

    const int & B::getB()
    { return ab.get(); }

    /***********main .cpp*********** ****/
    #include <iostream>
    using namespace std;

    #include "ab.h"

    int main()
    {
    B ab;
    cout << ab.getB() << endl;;
    }

    To compile and run them like this:
    c++ -Wall -o main main.cpp ab.cpp
    ls
    ab.cpp ab.h main main.cpp
    ./main
    19
    >
    /************End *************** ***/

    I hope anybody would like to tell me what cause it.
    Dose returning a const reference broke the
    dependency among header files?

    Now, I need your help, if you would like to tell me why,
    please cc your e-mail to me.
    Thank you,very much!

    Kermit
  • Sam

    #2
    Re: Returning a const reference brokes dependency among headerfiles?

    Kermit Mei writes:
    inline void A::set(int i) {
    a = i;
    }
    >
    inline const int & A::get() {
    return a;
    }
    Now, compile them and the errors occurs:
    >
    > c++ -Wall -o main main.cpp A.cpp B.cpp
    /tmp/ccB4KNX2.o: In function `B::getB()':
    B.cpp:(.text+0x d): undefined reference to `A::get()'
    /tmp/ccB4KNX2.o: In function `B::B()':
    B.cpp:(.text+0x 29): undefined reference to `A::set(int)'
    /tmp/ccB4KNX2.o: In function `B::B()':
    B.cpp:(.text+0x 45): undefined reference to `A::set(int)'
    collect2: ld returned 1 exit status
    Remove the inline keyword.


    -----BEGIN PGP SIGNATURE-----
    Version: GnuPG v1.4.9 (GNU/Linux)

    iEYEABECAAYFAkj YKBQACgkQx9p3GY HlUOJlXgCaA448f mpGGpwI9FH9nuZ9 q2nF
    IHgAnjY44723XVx KMP3jDMSrrflG5/wM
    =2hJ7
    -----END PGP SIGNATURE-----

    Comment

    • Kermit Mei

      #3
      Re: Returning a const reference brokes dependency among header files?

      On Sep 23, 7:19 am, Sam <s...@email-scan.comwrote:
      Kermit Mei writes:
      inline void A::set(int i) {
      a = i;
      }
      >
      inline const int & A::get() {
      return a;
      }
      Now, compile them and the errors occurs:
      >
      c++ -Wall -o main main.cpp A.cpp B.cpp
      /tmp/ccB4KNX2.o: In function `B::getB()':
      B.cpp:(.text+0x d): undefined reference to `A::get()'
      /tmp/ccB4KNX2.o: In function `B::B()':
      B.cpp:(.text+0x 29): undefined reference to `A::set(int)'
      /tmp/ccB4KNX2.o: In function `B::B()':
      B.cpp:(.text+0x 45): undefined reference to `A::set(int)'
      collect2: ld returned 1 exit status
      >
      Remove the inline keyword.
      >
      application_pgp-signature_part
      < 1KViewDownload
      Sam, it works well after remove them.

      But, sir, would you like to tell me why it can not be defined as
      "inline"?
      "inline" is just a suggestion for the compiler, isn't it? Then why
      there're
      something wrong with its arising? Why the compiler can NOT ignore it
      here?

      What's more, why it can be compiled with "inline" if A and B are
      defined and
      declared in a same file together?

      Thank you very much!

      Comment

      • Sam

        #4
        Re: Returning a const reference brokes dependency among headerfiles?

        Kermit Mei writes:
        On Sep 23, 7:19 am, Sam <s...@email-scan.comwrote:
        >Kermit Mei writes:
        inline void A::set(int i) {
        a = i;
        }
        >>
        inline const int & A::get() {
        return a;
        }
        Now, compile them and the errors occurs:
        >>
        > c++ -Wall -o main main.cpp A.cpp B.cpp
        /tmp/ccB4KNX2.o: In function `B::getB()':
        B.cpp:(.text+0x d): undefined reference to `A::get()'
        /tmp/ccB4KNX2.o: In function `B::B()':
        B.cpp:(.text+0x 29): undefined reference to `A::set(int)'
        /tmp/ccB4KNX2.o: In function `B::B()':
        B.cpp:(.text+0x 45): undefined reference to `A::set(int)'
        collect2: ld returned 1 exit status
        >>
        >Remove the inline keyword.
        >>
        > application_pgp-signature_part
        >< 1KViewDownload
        >
        Sam, it works well after remove them.
        >
        But, sir, would you like to tell me why it can not be defined as
        "inline"?
        "inline" is just a suggestion for the compiler, isn't it? Then why
        there're
        something wrong with its arising? Why the compiler can NOT ignore it
        here?
        This is a vast oversimplificat ion, but the 'inline' keyword requires that
        every module, every "translatio n unit" that references the inlined function,
        must have the definition of the function. Here, other modules, other
        translation units, do not define the function with the inline keyword.

        'inline' is an "all or nothing" proposition. Either you define the inline
        function everywhere, and everywhere the function is used, the function's
        code must be defined (and it must be the same in all translation units), or
        you cannot use the 'inline' keyword, in any definition of the function.
        That's why inline functions are typically declared in header files only,
        with the function's code written out.

        This is a gross oversimplificat ion, but that's the general idea.


        -----BEGIN PGP SIGNATURE-----
        Version: GnuPG v1.4.9 (GNU/Linux)

        iEYEABECAAYFAkj YXokACgkQx9p3GY HlUOL9ywCffeE0s 6y99ra7UTULRFNv d9mw
        l30An0S3WIsLODD d5jQLEL4COBDSo5 Vu
        =sC7x
        -----END PGP SIGNATURE-----

        Comment

        Working...