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::operator<<
<std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char>&, char const*)' LocalRef.o(.text+0xee):LocalRef.cpp: undefined reference to
`std::ostream::operator<<(int)'
LocalRef.o(.text+0xf7):LocalRef.cpp: undefined reference to
`std::basic_ostream<char, std::char_traits<char> >& std::operator<<
<std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char>&, char const*)'
LocalRef.o(.text+0x100):LocalRef.cpp: undefined reference to
`std::ostream::operator<<(int)'
LocalRef.o(.text+0x109):LocalRef.cpp: undefined reference to
`std::ostream::operator<<(std::ostream& (*)(std::ostream&))'
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()'