![]() |
#2
yangwawa2016-12-10 23:46
首先您重载了"<<"时候 为什么不用ostream 对象out 输出,而是采用cout;
我用的是VC6 将<<的友元函数定义成内联的函数就可以用了 #include <iostream> using namespace std; static int count=1; //静态变量,用于计算矩形的个数 template< class T > class MyRectangle { private: T length; //定义长 T width; //定义宽 T area; //定义面积 T circumference; //定义周长 public: /***************************构造函数**************************************/ MyRectangle(); //无参数构造函数 MyRectangle(T l, T w); //带长、宽构造函数 MyRectangle(const MyRectangle ©ed); //拷贝构造函数 /***************************析构函数**************************************/ ~MyRectangle(); /****************************外部借口*************************************/ void SetLength(T l); //设置长度 void SetWidth(T w) ; //设置宽度 T GetLength() const; //获取长度 T GetWidth() const; //获取宽度 T GetArea() const; //获取面积 T GetCircumference() const; //获取周长 void ShowRectangle() const; //打印矩形信息 /******************************重载运算符**************************************/ MyRectangle<T>& operator =(const MyRectangle<T> ©ed); //重载 = friend istream& operator >>(istream& in, MyRectangle<T> &r); //重载 >> friend ostream& operator <<(ostream& out, const MyRectangle<T> &r) //重载 << { out<<"矩形的长度是"<<r.length<<endl; out<<"矩形的宽度是"<<r.width<<endl; out<<"矩形的面积是"<<r.area<<endl; out<<"矩形的周长是"<<r.circumference<<endl; return out; } }; /******************************无参数构造函数***************************************/ template<class T> MyRectangle<T>::MyRectangle() { length=0; width=0; area=0; circumference=0; cout<<"调用第"<<count<<"个构造函数(不带参数)"<<endl; count++; } /******************************带参数构造函数***************************************/ template<class T> MyRectangle<T>::MyRectangle(T l, T w) { length=l; width=w; area=l*w; circumference=2*(l+w); cout<<"调用第"<<count<<"个构造函数(带参数)"<<endl; count++; } /******************************拷贝构造函数***************************************/ template<class T> MyRectangle<T>::MyRectangle(const MyRectangle<T>& copyed) { length=copyed.length; width=copyed.width; area=copyed.area; circumference=copyed.circumference; cout<<"调用第"<<count<<"个构造函数(拷贝构造函数)"<<endl; count++; } /******************************析构函数****************************************/ template<class T> MyRectangle<T>::~MyRectangle() { cout<<"调用第"<<count<<"个析构函数"<<endl; count--; } /******************************设置矩形的长度****************************************/ template<class T> void MyRectangle<T>::SetLength(T l) { length=l; } /******************************设置矩形的宽度****************************************/ template <class T> void MyRectangle<T>:: SetWidth(T w) { width=w; } /*****************************a*获取矩形的长度****************************************/ template<class T> T MyRectangle<T>::GetLength() const { return length; } /******************************获取矩形的宽度****************************************/ template<class T> T MyRectangle<T>::GetWidth() const { return width; } /******************************获取矩形的面积****************************************/ template<class T> T MyRectangle<T>:: GetArea() const { return area; } /******************************获取矩形的面积****************************************/ template<class T> T MyRectangle<T>:: GetCircumference() const { return circumference; } /******************************打印矩形的信息****************************************/ template<class T> void MyRectangle<T>:: ShowRectangle() const { std::cout<<"矩形的长度是"<<length<<endl; std::cout<<"矩形的宽度是"<<width<<endl; std::cout<<"矩形的面积是"<<area<<endl; std::cout<<"矩形的周长是"<<circumference<<endl; } /****************************** 重载 = ****************************************/ template<class T> MyRectangle<T>& MyRectangle<T>:: operator =(const MyRectangle<T> ©ed) { if(*this==copyed) return *this; this->length=copyed.length; this->width=copyed.width; this->area=copyed.area; this->circumference=copyed.circumference; return *this; } /****************************** 重载 >>**************************************/ template<class T> istream& operator >>(istream& in, MyRectangle<T> &r) { std::cout<<"请输入矩形的长度与宽度: "; std::cin>>r.length>>r.width; std::cout<<"输入成功"<<endl; return in; } /****************************** 重载 << *************************************/ /*template<class T> ostream& operator <<(ostream& out, const MyRectangle<T> &r) { out<<"矩形的长度是"<<r.length<<endl; out<<"矩形的宽度是"<<r.width<<endl; out<<"矩形的面积是"<<r.area<<endl; out<<"矩形的周长是"<<r.circumference<<endl; return out; }*/ int main() { MyRectangle<int> r1,r(5,3); MyRectangle<double> r2(5,8); MyRectangle<double> r3=r2; MyRectangle<int> r4(r1); r2.ShowRectangle(); cout<<r4; //这一句有错误 只有本站会员才能查看附件,请 登录 system("pause"); return 0; } |
#include <iostream>
using namespace std;
static int count=1; //静态变量,用于计算矩形的个数
template< class T >
class MyRectangle
{
private:
T length; //定义长
T width; //定义宽
T area; //定义面积
T circumference; //定义周长
public:
/***************************构造函数**************************************/
MyRectangle(); //无参数构造函数
MyRectangle(T l, T w); //带长、宽构造函数
MyRectangle(const MyRectangle ©ed); //拷贝构造函数
/***************************析构函数**************************************/
~MyRectangle();
/****************************外部借口*************************************/
void SetLength(T l); //设置长度
void SetWidth(T w) ; //设置宽度
T GetLength() const; //获取长度
T GetWidth() const; //获取宽度
T GetArea() const; //获取面积
T GetCircumference() const; //获取周长
void ShowRectangle() const; //打印矩形信息
/******************************重载运算符**************************************/
MyRectangle<T>& operator =(const MyRectangle<T> ©ed); //重载 =
friend istream& operator >>(istream& in, MyRectangle<T> &r); //重载 >>
friend ostream& operator <<(ostream& out, const MyRectangle<T> &r) ; //重载 <<
};
/******************************无参数构造函数***************************************/
template<class T>
MyRectangle<T>::MyRectangle()
{
length=0;
width=0;
area=0;
circumference=0;
cout<<"调用第"<<count<<"个构造函数(不带参数)"<<endl;
count++;
}
/******************************带参数构造函数***************************************/
template<class T>
MyRectangle<T>::MyRectangle(T l, T w)
{
length=l;
width=w;
area=l*w;
circumference=2*(l+w);
cout<<"调用第"<<count<<"个构造函数(带参数)"<<endl;
count++;
}
/******************************拷贝构造函数***************************************/
template<class T>
MyRectangle<T>::MyRectangle(const MyRectangle<T>& copyed)
{
length=copyed.length;
width=copyed.width;
area=copyed.area;
circumference=copyed.circumference;
cout<<"调用第"<<count<<"个构造函数(拷贝构造函数)"<<endl;
count++;
}
/******************************析构函数****************************************/
template<class T>
MyRectangle<T>::~MyRectangle()
{
cout<<"调用第"<<count<<"个析构函数"<<endl;
count--;
}
/******************************设置矩形的长度****************************************/
template<class T>
void MyRectangle<T>::SetLength(T l)
{
length=l;
}
/******************************设置矩形的宽度****************************************/
template <class T>
void MyRectangle<T>:: SetWidth(T w)
{
width=w;
}
/*****************************a*获取矩形的长度****************************************/
template<class T>
T MyRectangle<T>::GetLength() const
{
return length;
}
/******************************获取矩形的宽度****************************************/
template<class T>
T MyRectangle<T>::GetWidth() const
{
return width;
}
/******************************获取矩形的面积****************************************/
template<class T>
T MyRectangle<T>:: GetArea() const
{
return area;
}
/******************************获取矩形的面积****************************************/
template<class T>
T MyRectangle<T>:: GetCircumference() const
{
return circumference;
}
/******************************打印矩形的信息****************************************/
template<class T>
void MyRectangle<T>:: ShowRectangle() const
{
std::cout<<"矩形的长度是"<<length<<endl;
std::cout<<"矩形的宽度是"<<width<<endl;
std::cout<<"矩形的面积是"<<area<<endl;
std::cout<<"矩形的周长是"<<circumference<<endl;
}
/****************************** 重载 = ****************************************/
template<class T>
MyRectangle<T>& MyRectangle<T>:: operator =(const MyRectangle<T> ©ed)
{
if(*this==copyed)
return *this;
this->length=copyed.length;
this->width=copyed.width;
this->area=copyed.area;
this->circumference=copyed.circumference;
return *this;
}
/****************************** 重载 >>**************************************/
template<class T>
istream& operator >>(istream& in, MyRectangle<T> &r)
{
std::cout<<"请输入矩形的长度与宽度: ";
std::cin>>r.length>>r.width;
std::cout<<"输入成功"<<endl;
return in;
}
/****************************** 重载 << *************************************/
template<class T>
ostream& operator <<(ostream& out, const MyRectangle<T> &r)
{
cout<<"矩形的长度是"<<r.length<<endl;
cout<<"矩形的宽度是"<<r.width<<endl;
cout<<"矩形的面积是"<<r.area<<endl;
cout<<"矩形的周长是"<<r.circumference<<endl;
return out;
}
int main()
{
MyRectangle<int> r1,r(5,3);
MyRectangle<double> r2(5,8);
MyRectangle<double> r3=r2;
MyRectangle<int> r4(r1);
r2.ShowRectangle();
//cout<<r4; //这一句有错误
system("pause");
}
主函数中多了 cout<<r4;这一句就运行不了:
错误 2 error LNK1120: 1 个无法解析的外部命令
错误 1 error LNK2019: 无法解析的外部符号 "class std::basic_ostream<char,struct std::char_traits<char> > & __cdecl operator<<(class std::basic_ostream<char,struct std::char_traits<char> > &,class MyRectangle<int> const &)" (??6@YAAAV?$basic_ostream@DU?$char_traits@D@std@@@std@@AAV01@ABV?$MyRectangle@H@@@Z),该符号在函数 _main 中被引用
请问重载符号>> 和<< 错在哪了?