主题:这个很急,在线等!
#include<iostream>
#include<cstdlib>
using namespace std;
class vector
{ public :
vector( int ) ;
~vector();
int & operator[] ( int i ) ;
friend ostream & operator << ( ostream & output , vector & ) ;
friend istream & operator >> ( istream & input, vector & ) ;
int getlen(){return len;}
private :
int * v ;
int len ;
};
vector::vector( int size )
{ if (size <= 0 || size > 100 )
{ cout << "The size of " << size << " is null !\n" ; abort() ; }
v = new int[ size ] ; len = size ;
}
vector :: ~vector() { delete[] v ; len = 0 ; }
int & vector :: operator [] ( int i )
{ if( i >=0 && i < len )
return v[ i ] ;
cout << "The subscript " << i << " is outside !\n" ;
abort() ;
}
ostream & operator << ( ostream & output, vector & ary )
{
for(int i = 0 ; i < ary.getlen() ; i ++ )
output << ary[ i ] << " " ;
output << endl ;
return output ;
}
istream & operator >> ( istream & input, vector & ary )
{
for( int i = 0 ; i < ary.getlen() ; i ++ )
input >> ary[ i ] ;
return input ;
}
int main()
{
int k ;
cout << "Input the length of vector A :\n" ;
cin >> k ;
vector A( k ) ;
cout << "Input the elements of vector A :\n" ;
operator >> (cin, A);// 问题在哪里?
cout << "Output the elements of vector A :\n" ;
operator << (cout, A);// 问题在哪里?
return 1;
}
--------------------Configuration: 代码练习 - Win32 Debug--------------------
Compiling...
1.cpp
D:\学习资料\c++\运算符重载\代码练习\1.cpp(55) : error C2668: '>>' : ambiguous call to overloaded function
D:\学习资料\c++\运算符重载\代码练习\1.cpp(59) : error C2668: '<<' : ambiguous call to overloaded function
执行 cl.exe 时出错.
1.obj - 1 error(s), 0 warning(s)
#include<cstdlib>
using namespace std;
class vector
{ public :
vector( int ) ;
~vector();
int & operator[] ( int i ) ;
friend ostream & operator << ( ostream & output , vector & ) ;
friend istream & operator >> ( istream & input, vector & ) ;
int getlen(){return len;}
private :
int * v ;
int len ;
};
vector::vector( int size )
{ if (size <= 0 || size > 100 )
{ cout << "The size of " << size << " is null !\n" ; abort() ; }
v = new int[ size ] ; len = size ;
}
vector :: ~vector() { delete[] v ; len = 0 ; }
int & vector :: operator [] ( int i )
{ if( i >=0 && i < len )
return v[ i ] ;
cout << "The subscript " << i << " is outside !\n" ;
abort() ;
}
ostream & operator << ( ostream & output, vector & ary )
{
for(int i = 0 ; i < ary.getlen() ; i ++ )
output << ary[ i ] << " " ;
output << endl ;
return output ;
}
istream & operator >> ( istream & input, vector & ary )
{
for( int i = 0 ; i < ary.getlen() ; i ++ )
input >> ary[ i ] ;
return input ;
}
int main()
{
int k ;
cout << "Input the length of vector A :\n" ;
cin >> k ;
vector A( k ) ;
cout << "Input the elements of vector A :\n" ;
operator >> (cin, A);// 问题在哪里?
cout << "Output the elements of vector A :\n" ;
operator << (cout, A);// 问题在哪里?
return 1;
}
--------------------Configuration: 代码练习 - Win32 Debug--------------------
Compiling...
1.cpp
D:\学习资料\c++\运算符重载\代码练习\1.cpp(55) : error C2668: '>>' : ambiguous call to overloaded function
D:\学习资料\c++\运算符重载\代码练习\1.cpp(59) : error C2668: '<<' : ambiguous call to overloaded function
执行 cl.exe 时出错.
1.obj - 1 error(s), 0 warning(s)