| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1272 人关注过本帖
标题:请问重载符号>> 和<< 错在哪了?
只看楼主 加入收藏
很好吃
Rank: 1
等 级:新手上路
帖 子:4
专家分:0
注 册:2015-10-14
结帖率:0
收藏
已结贴  问题点数:10 回复次数:2 
请问重载符号>> 和<< 错在哪了?
#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 &copyed);            //拷贝构造函数


    /***************************析构函数**************************************/
    ~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> &copyed);                        //重载 =
    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> &copyed)                        
{
    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 中被引用   


请问重载符号>> 和<< 错在哪了?
搜索更多相关主题的帖子: private include public count 
2016-12-10 22:32
yangwawa
Rank: 2
等 级:论坛游民
威 望:3
帖 子:15
专家分:67
注 册:2016-11-26
收藏
得分:5 
首先您重载了"<<"时候 为什么不用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 &copyed);            //拷贝构造函数


    /***************************析构函数**************************************/
    ~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> &copyed);                        //重载 =
    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> &copyed)                        
{
    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;

}
2016-12-10 23:46
rjsp
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:版主
威 望:528
帖 子:9007
专家分:53942
注 册:2011-1-18
收藏
得分:5 
无关的代码就别贴了,你一大坨代码,大部人看一眼就关了
如果我是你,我只贴这么多
程序代码:
#include <iostream>

template<class T> struct foo
{
    friend std::ostream& operator <<( std::ostream& out, const foo<T>& f );
};

template<class T>
std::ostream& operator <<( std::ostream& out, const foo<T>& f )
{
    return out;
}

int main( void )
{
    foo<int> f;
    std::cout << f;
}
报错信息是:……(略,我就不拷贝粘贴了)……


这样,别人一眼就能看出你的错误,声明了一个普通函数作为友元,却没有找到其定义。
程序代码:
#include <iostream>

template<class T> struct foo;
template<class T> std::ostream& operator <<( std::ostream& out, const foo<T>& f );

template<class T> struct foo
{
    friend std::ostream& operator << <T>( std::ostream& out, const foo<T>& f );
};

template<class T> std::ostream& operator <<( std::ostream& out, const foo<T>& f )
{
    return out;
}

int main( void )
{
    foo<int> f;
    std::cout << f;
}

2016-12-12 08:44
快速回复:请问重载符号>> 和<< 错在哪了?
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.017930 second(s), 9 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved