one overload question

Discussion in 'C++' started by Hunter Hou, Jun 14, 2004.

  1. Hunter Hou

    Hunter Hou Guest

    Hi,

    I have this program, compiling is passed, but when i wanted to get the
    executable file(g++ -o output input.o), it fails.

    Who can lead me where the error is?

    Thanks,
    Hunter
    ===


    #include <iostream>


    class Rational {

    public:

    Rational( int par1 = 0, int par2 = 1 ): n( par1 ), d( par2 ) {}

    void printRational( );


    int getN() const { return n; }

    int getD() const { return d; }

    void setN( int par ) { n = par; }

    void setD( int par ) { d = par; }

    public:

    friend const Rational& operator * ( const Rational& par1,
    const Rational& par2 );

    private:

    int n, d;
    };

    const Rational& operator * ( const Rational& rat1, const Rational& rat2 )
    {

    // I know this solution is bad and there's a correct one, I just want to
    try what Scott Mayers says.
    static Rational result;

    result.setN( rat1.getN() * rat2.getN() ) ;
    result.setD( rat1.getD() * rat2.getD() ) ;

    return result;
    }

    void Rational::printRational() {

    std::cout << "the N is: " << n << "the D is: " << d << std::endl;

    }


    int main()
    {

    Rational rat1( 2, 3 ), rat2( 4, 5 );

    Rational result = rat1 * rat2;

    result.printRational();

    return 0;

    }


    LocalRef.o(.text+0xba):LocalRef.cpp: undefined reference to
    `std::basic_ostream<char, std::char_traits<char> >& std::endl<char,
    std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char>
    >&)'

    LocalRef.o(.text+0xe0):LocalRef.cpp: undefined reference to `std::cout'
    LocalRef.o(.text+0xe5):LocalRef.cpp: undefined reference to
    `std::basic_ostream<char, std::char_traits<char> >& std::eek:perator<<
    <std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char>
    >&, char const*)'

    LocalRef.o(.text+0xee):LocalRef.cpp: undefined reference to
    `std::eek:stream::eek:perator<<(int)'
    LocalRef.o(.text+0xf7):LocalRef.cpp: undefined reference to
    `std::basic_ostream<char, std::char_traits<char> >& std::eek:perator<<
    <std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char>
    >&, char const*)'

    LocalRef.o(.text+0x100):LocalRef.cpp: undefined reference to
    `std::eek:stream::eek:perator<<(int)'
    LocalRef.o(.text+0x109):LocalRef.cpp: undefined reference to
    `std::eek:stream::eek:perator<<(std::eek:stream& (*)(std::eek:stream&))'
    LocalRef.o(.text+0x1a8):LocalRef.cpp: undefined reference to
    `std::ios_base::Init::Init()'
    LocalRef.o(.text+0x1c7):LocalRef.cpp: undefined reference to
    `std::ios_base::Init::~Init()'
    Hunter Hou, Jun 14, 2004
    #1
    1. Advertising

  2. Hunter Hou

    Rolf Magnus Guest

    Hunter Hou wrote:

    > Hi,
    >
    > I have this program, compiling is passed, but when i wanted to get the
    > executable file(g++ -o output input.o), it fails.
    >
    > Who can lead me where the error is?



    Why do you think this is an "overload question"? Anyway, it looks to me
    as if your linker doesn't find the symbols of the standard library. Are
    you sure you used g++ for linking, not gcc, and that you linked your
    code using the same g++ version you compiled it with?
    Rolf Magnus, Jun 14, 2004
    #2
    1. Advertising

  3. Hunter Hou

    Hunter Hou Guest

    Rolf Magnus wrote:
    > Hunter Hou wrote:
    >
    >
    >>Hi,
    >>
    >>I have this program, compiling is passed, but when i wanted to get the
    >>executable file(g++ -o output input.o), it fails.
    >>
    >>Who can lead me where the error is?

    >
    >
    >
    > Why do you think this is an "overload question"? Anyway, it looks to me
    > as if your linker doesn't find the symbols of the standard library. Are
    > you sure you used g++ for linking, not gcc, and that you linked your
    > code using the same g++ version you compiled it with?
    >


    In fact, I don't know if this is really caused by overload.
    I've been using g++ of mingw and never run into such a confusing problem.
    Hunter Hou, Jun 14, 2004
    #3
  4. Hunter Hou

    lallous Guest

    "Hunter Hou" <> wrote in message
    news:cajnt0$...
    > Hi,
    >
    > I have this program, compiling is passed, but when i wanted to get the
    > executable file(g++ -o output input.o), it fails.
    >
    > Who can lead me where the error is?
    >
    > Thanks,
    > Hunter
    > ===

    <snip>
    >
    > LocalRef.o(.text+0xba):LocalRef.cpp: undefined reference to
    > `std::basic_ostream<char, std::char_traits<char> >& std::endl<char,
    > std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char>
    > >&)'

    > LocalRef.o(.text+0xe0):LocalRef.cpp: undefined reference to `std::cout'
    > LocalRef.o(.text+0xe5):LocalRef.cpp: undefined reference to
    > `std::basic_ostream<char, std::char_traits<char> >& std::eek:perator<<
    > <std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char>
    > >&, char const*)'

    > LocalRef.o(.text+0xee):LocalRef.cpp: undefined reference to
    > `std::eek:stream::eek:perator<<(int)'
    > LocalRef.o(.text+0xf7):LocalRef.cpp: undefined reference to
    > `std::basic_ostream<char, std::char_traits<char> >& std::eek:perator<<
    > <std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char>
    > >&, char const*)'

    > LocalRef.o(.text+0x100):LocalRef.cpp: undefined reference to
    > `std::eek:stream::eek:perator<<(int)'
    > LocalRef.o(.text+0x109):LocalRef.cpp: undefined reference to
    > `std::eek:stream::eek:perator<<(std::eek:stream& (*)(std::eek:stream&))'
    > LocalRef.o(.text+0x1a8):LocalRef.cpp: undefined reference to
    > `std::ios_base::Init::Init()'
    > LocalRef.o(.text+0x1c7):LocalRef.cpp: undefined reference to
    > `std::ios_base::Init::~Init()'
    >


    I had this problem before, but didn't resolve yet.
    Try this simple application and see if it compile / links too:

    #include <iostream>

    using namespace std;

    int main()
    {
    cout << "Hello world!\n" << endl;
    return 0;
    }

    --
    Elias
    lallous, Jun 14, 2004
    #4
  5. Hunter Hou

    Rolf Magnus Guest

    Hunter Hou wrote:

    > Rolf Magnus wrote:
    >> Hunter Hou wrote:
    >>
    >>
    >>>Hi,
    >>>
    >>>I have this program, compiling is passed, but when i wanted to get
    >>>the executable file(g++ -o output input.o), it fails.
    >>>
    >>>Who can lead me where the error is?

    >>
    >>
    >>
    >> Why do you think this is an "overload question"? Anyway, it looks to
    >> me as if your linker doesn't find the symbols of the standard
    >> library. Are you sure you used g++ for linking, not gcc, and that you
    >> linked your code using the same g++ version you compiled it with?
    >>

    >
    > In fact, I don't know if this is really caused by overload.
    > I've been using g++ of mingw and never run into such a confusing
    > problem.


    The linker error message reports things like endl, cout and the stream
    operators (actually everything from the standard library that you're
    using) being missing. There seems to be no connection to the fact that
    you're overloading operator*.
    Rolf Magnus, Jun 14, 2004
    #5
  6. Hunter Hou

    Hunter Hou Guest

    lallous wrote:


    > I had this problem before, but didn't resolve yet.
    > Try this simple application and see if it compile / links too:
    >
    > #include <iostream>
    >
    > using namespace std;
    >
    > int main()
    > {
    > cout << "Hello world!\n" << endl;
    > return 0;
    > }
    >
    > --
    > Elias
    >


    My test.cpp is as exactly same as yours, it works.That's why the above
    error is confusing me.

    Hunter
    Hunter Hou, Jun 14, 2004
    #6
  7. "Hunter Hou" <> wrote in message
    news:cak36i$...
    > lallous wrote:
    >
    >
    > > I had this problem before, but didn't resolve yet.
    > > Try this simple application and see if it compile / links too:
    > >
    > > #include <iostream>
    > >
    > > using namespace std;
    > >
    > > int main()
    > > {
    > > cout << "Hello world!\n" << endl;
    > > return 0;
    > > }
    > >
    > > --
    > > Elias
    > >

    >
    > My test.cpp is as exactly same as yours, it works.That's why the above
    > error is confusing me.
    >


    OK well start taking the obvious troubleshooting steps. Gradually cut out
    code from the program that doesn't work, until you have a hello world
    program. When it starts working again you know that the last thing you
    removed was the problem.

    Personally I don't think there is anything wrong with your code, I would
    expect there is something wrong with the way you are compiling and linking
    your program or something wrong with the way you have installed your
    compiler.

    John
    John Harrison, Jun 14, 2004
    #7
    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. news.frontiernet.net
    Replies:
    6
    Views:
    1,118
    news.frontiernet.net
    Apr 16, 2004
  2. loveNUNO
    Replies:
    2
    Views:
    907
    loveNUNO
    Nov 20, 2003
  3. Piotre Ugrumov
    Replies:
    3
    Views:
    368
    Nick Hounsome
    Jan 25, 2004
  4. Merciadri Luca
    Replies:
    4
    Views:
    808
  5. Ying-Chieh Liao

    function overload (not operator overload)

    Ying-Chieh Liao, Oct 11, 2004, in forum: Perl Misc
    Replies:
    3
    Views:
    248
    Sherm Pendley
    Oct 11, 2004
Loading...

Share This Page