求救 编写模板类出现的错误不知道该怎样修改

zxhyxwwu 2008-07-13 04:03:30
问题描述:我想编一个类来实现复数的加减乘,比较,并尝试着用模板类来实现,以便可以处理不同的数据类型。

源代码:

#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]
...全文
168 8 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
zxhyxwwu 2008-07-27
  • 打赏
  • 举报
回复
我是发friend ostream& operator < <( ostream &output, const complex <T> &operand ); 改为
template<class TT> friend ostream& operator < <( ostream &output, const complex <TT> &operand ); 然后就可以了,也不知道是为什么,我想他的参数是另外一种类型的参数,而该函数本身又不属于complex类,他的参数应该与类的参数有区别。不知道是不是这样

SCAUniaodan 2008-07-21
  • 打赏
  • 举报
回复
根据1,2楼的教导,我把实现放到一个文件中就可以了。但原理不明白。谢谢!哪位出来讲解易懂一些?
谢谢
zxhyxwwu 2008-07-17
  • 打赏
  • 举报
回复
"operator" 和“<<”是不能分开写的,要那样写的话就变成重载"<"了
lily604 2008-07-13
  • 打赏
  • 举报
回复
可以把实现文件改为一个头文件,然后在这个头文件包含原来的头文件
xiabing1003 2008-07-13
  • 打赏
  • 举报
回复
试试“template < class T > ostream &operator<T> < <( ostream &output, const complex <T> &operand ) ”
zxhyxwwu 2008-07-13
  • 打赏
  • 举报
回复
模板实现写到.h文件中,但是还没有调试成功啊,后来自己修改了一下,错误就少多了:
#ifndef COMPLEX_H
#define COMPLEX_H

#include<iostream>
using std::ostream;
template < class T >
class complex;

template <class T> class complex
{ friend ostream& operator<<( ostream &output, const complex<T> &operand );
public:
complex( T = 0.0, T = 0.0 );
complex<T> operator+( const complex<T> & ) const;
complex<T> operator-( const complex<T> & ) const;

complex<T> 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<T> complex< T >::operator+( const complex<T> &operand ) const
{ return complex( real + operand.real, imaginary + operand.imaginary );
}


template < class T > complex<T> complex< T >::operator-( const complex<T> &operand ) const
{ return complex( real - operand.real, imaginary - operand.imaginary );
}


template < class T > complex<T> 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 >
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< double > y1(2.2,3.3);
complex< double > y2(4.5,8.5);
complex< double > 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;
}
但是又出现新的问题:
10 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

[Linker error] undefined reference to `operator<<(std::ostream&, complex<int> const&)'

[Linker error] undefined reference to `operator<<(std::ostream&, complex<double> const&)'

10 D:\vc++\dec++\混合\complex_lxf.cpp ld returned 1 exit status

希望大家帮助看看,一同进步……
K行天下 2008-07-13
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 akirya 的回复:]
将模板的实现写到.h文件中
[/Quote]
up
现在的编译器不支持模板声明与实现的分离 , 新标准有export关键字可以,但是编译器好像没有实现
  • 打赏
  • 举报
回复
将模板的实现写到.h文件中

65,187

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧