求救 编写模板类出现的错误不知道该怎样修改
问题描述:我想编一个类来实现复数的加减乘,比较,并尝试着用模板类来实现,以便可以处理不同的数据类型。
源代码:
#ifndef COMPLEX_H
#define COMPLEX_H
#include<iostream>
using std::ostream;
template <class T>
class complex
{ friend ostream &operator<<( ostream &, const complex<T> & );
public:
complex( T &, T & );
complex operator+( const complex<T> & ) const;
complex operator-( const complex<T> & ) const;
complex operator*( const complex<T> & ) const;
bool operator==( const complex<T> & ) const;
bool operator!=( const complex<T> & ) const;
// void Print() const;
private:
T real;
T imaginary;
};
#endif
#include<iostream>
using std::cout;
using std::endl;
//#inlcude "complex.h"
template < class T >
complex<T>::complex( T &realprt, T &imaginaryprt )
:real( realprt ),imaginary( imaginaryprt )
{
}
template < class T >
complex complex< T >::operator+( const complex<T> &operand ) const
{ return complex( real + operand.real, imaginary + operand.imaginary );
}
template < class T >
complex complex< T >::operator-( const complex<T> &operand ) const
{ return complex( real - operand.real, imaginary - operand.imaginary );
}
template < class T >
complex complex< T >::operator*( const complex<T> &operand ) const
{ return complex( real*operand.real - imaginary*operand.imaginary,
real*operand.imaginary + imaginary*operand.real );
}
template < class T >
bool complex< T >::operator==( const complex<T> &operand ) const
{ if( real == operand.real && imaginary == operand.imaginary )
return true;
else
return false;
}
template < class T >
bool complex< T >::operator!=( const complex<T> &operand ) const
{ if( real != operand.real || imaginary != operand.imaginary )
return true;
else
return false;
}
/* template < class T >
complex< T >::Print() const
{ cout<<"( "<<real<<","<<imaginary<<" )";
} */
//template< class T >
ostream &operator<<( ostream &output, const complex<T> &operand )
{ cout<<"( "<<operand.real<<","<<operand.imaginary<<" )";
return output;
}
//difinition of main
#include<iostream>
using std::cout;
using std::cin;
using std::endl;
//include "complex.h"
int main()
{ complex< int > x1( 4, 5 );
complex< int > x2( 8, 9 );
complex< int > x3( 4, 5 );
complex< float > y1(2.2,3,3);
complex< float > y2(4.5,8.5);
complex< float > y3(2.2,3.3);
cout<<"x1 + x2 = "<<(x1 + x2 )<<endl;
cout<<"y1 + y2 = "<<(y1 + y2 )<<endl;
if( x1 == x2 )
cout<<"x1 and x2 are equal\n";
else
cout<<"x1 and x2 are unequal\n";
if(x2 != x3 )
cout<<"x2 and x3 are not equal\n";
if(y1 == y3 )
cout<<"y1 and y3 are equal\n";
if(y2 != y3 )
cout<<"y2 and y3 are not equal\n";
system("pause");
return 0;
}
错误提示:
9 D:\vc++\dec++\混合\complex_lxf.cpp [Warning] friend declaration `std::ostream& operator<<(std::ostream&, const complex<T>&)' declares a non-template function
9 D:\vc++\dec++\混合\complex_lxf.cpp [Warning] (if this is not what you intended, make sure the function template has already been declared and add <> after the function name here) -Wno-non-template-friend disables this warning
40 D:\vc++\dec++\混合\complex_lxf.cpp expected constructor, destructor, or type conversion before "complex"
40 D:\vc++\dec++\混合\complex_lxf.cpp expected `;' before "complex"
45 D:\vc++\dec++\混合\complex_lxf.cpp expected constructor, destructor, or type conversion before "complex"
45 D:\vc++\dec++\混合\complex_lxf.cpp expected `;' before "complex"
50 D:\vc++\dec++\混合\complex_lxf.cpp expected constructor, destructor, or type conversion before "complex"
50 D:\vc++\dec++\混合\complex_lxf.cpp expected `;' before "complex"
76 D:\vc++\dec++\混合\complex_lxf.cpp `T' was not declared in this scope
76 D:\vc++\dec++\混合\complex_lxf.cpp template argument 1 is invalid
77 D:\vc++\dec++\混合\complex_lxf.cpp ISO C++ forbids declaration of `operand' with no type
D:\vc++\dec++\混合\complex_lxf.cpp In function `std::ostream& operator<<(std::ostream&, const int&)':
77 D:\vc++\dec++\混合\complex_lxf.cpp `real' has not been declared
77 D:\vc++\dec++\混合\complex_lxf.cpp request for member of non-aggregate type before '<<' token
77 D:\vc++\dec++\混合\complex_lxf.cpp `imaginary' has not been declared
77 D:\vc++\dec++\混合\complex_lxf.cpp request for member of non-aggregate type before '<<' token
D:\vc++\dec++\混合\complex_lxf.cpp In function `int main()':
请能者帮我看一下,不胜感激![size=11px][/size]