|
这是书上的一个例题
- //HugeInt.h
- #ifndef HUGEINT_H
- #define HUGEINT_H
- #include<iostream>
- using namespace std;
-
- class HugeInt {
- friend ostream &operator<<( ostream &, HugeInt & );
- public:
- HugeInt( long = 0 );
- HugeInt( const char *);
- HugeInt operator+( HugeInt & );
- HugeInt operator+( int );
- HugeInt operator+( const char * );
- private:
- short integer[30];
- };
-
- #endif
复制代码
- //HugeInt.cpp
- #include"HugeInt.h"
-
- HugeInt::HugeInt( long val )
- {
- int i;
- for ( i = 0; i <=29; i++ )
- integer[ i ] = val % 10;
- for ( i = 29; val != 0 && i >=0; i-- ){
- integer[ i ] = val % 10;
- val /= 10;
- }
- }
-
- HugeInt::HugeInt( const char * string)
- {
- int i, j;
- for ( i = 0; i <= 29; i++ )
- integer[ i ] = 0;
-
- for ( i = 30 - strlen( string ), j = 0; i <= 29; i++, j++)
- integer[ i ] = string[ j ] - '0';
- }
- HugeInt HugeInt::operator+( HugeInt &op2 )
- {
- HugeInt temp;
- int carry = 0;
- for ( int i = 29; i >= 0; i-- ) {
- temp.integer[ i ] = integer[ i ] + op2.integer[ i ] + carry;
- if ( temp.integer[ i ] > 9 ) {
- temp.integer[ i ] %= 10;
- carry = 1;
- }
- else
- carry = 0;
- }
- return temp;
- }
- HugeInt HugeInt::operator+( int op2 )
- { return *this + HugeInt( op2 );}
- HugeInt HugeInt::operator+( const char * op2 )
- { return *this + HugeInt ( op2);}
- ostream & operator<<( ostream & output, HugeInt &num )
- {
- int i;
-
- for ( i = 0; ( num.integer[ i ] == 0 ) && ( i <=29 ); i++)
- ;
- if ( i == 30 )
- output << 0;
- else
- for ( ; i <= 29; i++ )
- output << num.integer[ i ];
- return output;
- }
复制代码
- //main.cpp
- #include"HugeInt.h"
- #include<iostream>
- using namespace std;
-
- int main()
- {
- HugeInt n1( 1234567890 ),
- n2( "9999999999999999" ),
- n3( "1" ),
- n4;
- cout << " n1 is " << n1 << "\nn2 is " << n2
- << " \nn2 is " << " \nn3 is " << n3
- << " \nn4 is " << " \n\n";
-
- n4 = n1 + n2;
- cout << n1 << " + " << n2 << " = " << n4 << "\n\n";
-
- cout << n2 << " + " << n3 << "\n= " << ( n2 + n3 )
- << "\n\n";
-
- n4 = n1 + HugeInt( 9L );
- cout << n1 << " + " << 9 << " = " << n4 << endl;
- return 0;
- }
复制代码
#gcc -S HugeInt.cpp
HugeInt.cpp: In member function `HugeInt HugeInt: perator+(int)':
HugeInt.cpp:42: error: no match for `HugeInt& + HugeInt' operator
HugeInt.cpp:26: error: candidates are: HugeInt HugeInt: perator+(HugeInt&)
HugeInt.cpp:42: error: HugeInt HugeInt: perator+(int)
HugeInt.h:14: error: HugeInt HugeInt: perator+(const char*)
HugeInt.cpp: In member function `HugeInt HugeInt: perator+(const char*)':
HugeInt.cpp:45: error: no match for `HugeInt& + HugeInt' operator
HugeInt.cpp:26: error: candidates are: HugeInt HugeInt: perator+(HugeInt&)
HugeInt.cpp:42: error: HugeInt HugeInt: perator+(int)
HugeInt.cpp:45: error: HugeInt HugeInt: perator+(const char*)
#gcc -S main.cpp
main.cpp: In function `int main()':
main.cpp:19: error: no match for `std::basic_ostream<char,
std::char_traits<char> >& << HugeInt' operator
/usr/include/c++/3.3/bits/ostream.tcc:63: error: candidates are:
std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT,
_Traits>: perator<<(std::basic_ostream<_CharT,
_Traits>&(*)(std::basic_ostream<_CharT, _Traits>&)) [with _CharT = char,
_Traits = std::char_traits<char>]
/usr/include/c++/3.3/bits/ostream.tcc:85: error:
std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT,
_Traits>: perator<<(std::basic_ios<_CharT,
_Traits>&(*)(std::basic_ios<_CharT, _Traits>&)) [with _CharT = char, _Traits
= std::char_traits<char>]
/usr/include/c++/3.3/bits/ostream.tcc:107: error:
std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT,
_Traits>::operator<<(std::ios_base&(*)(std::ios_base&)) [with _CharT = char,
_Traits = std::char_traits<char>]
/usr/include/c++/3.3/bits/ostream.tcc:179: error:
std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT,
_Traits>::operator<<(long int) [with _CharT = char, _Traits =
std::char_traits<char>]/usr/include/c++/3.3/bits/ostream.tcc:216: error:
std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT,
_Traits>::operator<<(long unsigned int) [with _CharT = char, _Traits =
std::char_traits<char>]
/usr/include/c++/3.3/bits/ostream.tcc:154: error:
std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT,
_Traits>::operator<<(bool) [with _CharT = char, _Traits =
std::char_traits<char>]
/usr/include/c++/3.3/ostream:178: error:
std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT,
_Traits>::operator<<(short int) [with _CharT = char, _Traits =
std::char_traits<char>]
/usr/include/c++/3.3/ostream:189: error:
std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT,
_Traits>::operator<<(short unsigned int) [with _CharT = char, _Traits =
std::char_traits<char>]
/usr/include/c++/3.3/ostream:193: error:
std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT,
_Traits>::operator<<(int) [with _CharT = char, _Traits =
std::char_traits<char>]
/usr/include/c++/3.3/ostream:193: error:
std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT,
_Traits>::operator<<(int) [with _CharT = char, _Traits =
std::char_traits<char>]
/usr/include/c++/3.3/ostream:204: error:
std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT,
_Traits>::operator<<(unsigned int) [with _CharT = char, _Traits =
std::char_traits<char>]
/usr/include/c++/3.3/bits/ostream.tcc:242: error:
std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT,
_Traits>::operator<<(long long int) [with _CharT = char, _Traits =
std::char_traits<char>]
/usr/include/c++/3.3/bits/ostream.tcc:280: error:
std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT,
_Traits>::operator<<(long long unsigned int) [with _CharT = char, _Traits =
std::char_traits<char>]
/usr/include/c++/3.3/bits/ostream.tcc:306: error:
std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT,
_Traits>::operator<<(double) [with _CharT = char, _Traits =
std::char_traits<char>]
/usr/include/c++/3.3/ostream:219: error:
std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT,
_Traits>::operator<<(float) [with _CharT = char, _Traits =
std::char_traits<char>]
/usr/include/c++/3.3/bits/ostream.tcc:331: error:
std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT,
_Traits>::operator<<(long double) [with _CharT = char, _Traits =
std::char_traits<char>]
/usr/include/c++/3.3/bits/ostream.tcc:356: error:
std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT,
_Traits>::operator<<(const void*) [with _CharT = char, _Traits =
std::char_traits<char>]
/usr/include/c++/3.3/bits/ostream.tcc:128: error:
std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT,
_Traits>::operator<<(std::basic_streambuf<_CharT, _Traits>*) [with _CharT =
char, _Traits = std::char_traits<char>]
HugeInt.h:8: error: std::ostream& operator<<(std::ostream&,
HugeInt&)
/usr/include/c++/3.3/ostream:491: error:
std::basic_ostream<char, _Traits>& std::operator<<(std::basic_ostream<char,
_Traits>&, const unsigned char*) [with _Traits = std::char_traits<char>]
/usr/include/c++/3.3/ostream:486: error:
std::basic_ostream<char, _Traits>& std::operator<<(std::basic_ostream<char,
_Traits>&, const signed char*) [with _Traits = std::char_traits<char>]
/usr/include/c++/3.3/bits/ostream.tcc:630: error:
std::basic_ostream<char, _Traits>& std::operator<<(std::basic_ostream<char,
_Traits>&, const char*) [with _Traits = std::char_traits<char>]
/usr/include/c++/3.3/bits/ostream.tcc:580: error:
std::basic_ostream<_CharT, _Traits>&
std::operator<<(std::basic_ostream<_CharT, _Traits>&, const char*) [with
_CharT = char, _Traits = std::char_traits<char>]
/usr/include/c++/3.3/ostream:452: error:
std::basic_ostream<char, _Traits>& std::operator<<(std::basic_ostream<char,
_Traits>&, unsigned char) [with _Traits = std::char_traits<char>]
/usr/include/c++/3.3/ostream:447: error:
std::basic_ostream<char, _Traits>& std::operator<<(std::basic_ostream<char,
_Traits>&, signed char) [with _Traits = std::char_traits<char>]
/usr/include/c++/3.3/bits/ostream.tcc:508: error:
std::basic_ostream<char, _Traits>& std::operator<<(std::basic_ostream<char,
_Traits>&, char) [with _Traits = std::char_traits<char>]
/usr/include/c++/3.3/ostream:436: error:
std::basic_ostream<_CharT, _Traits>&
std::operator<<(std::basic_ostream<_CharT, _Traits>&, char) [with _CharT =
char, _Traits = std::char_traits<char>]
main.cpp:21: error: no match for `HugeInt& + HugeInt' operator
HugeInt.h:12: error: candidates are: HugeInt HugeInt::operator+(HugeInt&) |
|