硫酸亚铜 发表于 2014-10-22 17:39:02

请教各位大虾重载<<运算符的问题

本帖最后由 硫酸亚铜 于 2014-10-22 17:41 编辑

代码如下:
#include <iostream>
using namespace std;
template <class T>
class Chain;
template <class T>
class ChainNode
{
    friend class Chain<T>;
private:
    T data;
    ChainNode<T>* link = 0 ;
};

template <class T>
class Chain
{
    friend ostream& operator<< <T>(ostream& out, const Chain<T>& x);
public:
    Chain(){ first = 0; }
    Chain<T>& InsertChange(int k);
private:
    ChainNode<T>* first;//指向第一个节点的指针
};
int main()
{
    Chain<int> test;
    for (int i = 0; i < 10; i++)
    {
      test.InsertChange(i);
    }
    cout << test; //此处报错!!!
    return 0;

}
//重载<<
template<class T>
ostream& operator<< (ostream& out, const Chain<T>& x)
{
    ChainNode<T>* p;
    for (p = x.first; p; p = p->link)
    {
      out << p->date << endl;
    }
    return out;
}
template <class T>
Chain<T>& Chain<T>::InsertChange(int k)
{
    ChainNode<T>* p = new ChainNode<T>;
    ChainNode<T>* q;
    p->data = k;
    if (!first)
    {
      first = p;
    }
    else
    {
      q = first;
      while (q->link)
      {
             q = q->link;
      }
         q->link = p;
    }
    return *this;
}

为什么我重载了<<运算符之后,在main函数中写入
“cout<<test”

这条语句之后就会提示我
“error LNK2019: 无法解析的外部符号 "class std::basic_ostream<char,struct std::char_traits<char> > & __cdecl std::<<(class std::basic_ostream<char,struct std::char_traits<char> > &,class Chain<int> const &)" (?<<@std@@YAAAV?$basic_ostream@DU?$char_traits@D@std@@@1@AAV21@ABV?$Chain@H@@@Z),该符号在函数 _main 中被引用
1>E:\project\chain\Debug\chain.exe : fatal error LNK1120: 1 个无法解析的外部命令”

想不明白,还请各位大虾不吝赐教!


yy57 发表于 2014-10-23 10:44:31

这个问题我也遇到了。解决的方法倒是有,但是我是无意间发现的,也不知道为什么要这么做。

<<作为友元重载,重载声明时可以在类前加上const 限定符,但是在重载定义时,类前加上const 限定符会报错,报错说明如你,在重载的定义时,把类前的const限定符去掉又好了。

所以你的重载定义那里改成
36.template<class T>

37.ostream& operator<< (ostream& out, Chain<T>& x)

38.{

39.    ChainNode<T>* p;

40.    for (p = x.first; p; p = p->link)

41.    {

42.      out << p->date << endl;

43.    }

44.    return out;

45.}

应该就好了。

我用的VS。。。如果有问题再圈我,今天容我查阅书籍再看看到底怎么回事。

硫酸亚铜 发表于 2014-10-23 18:45:00

yy57 发表于 2014-10-23 10:44
这个问题我也遇到了。解决的方法倒是有,但是我是无意间发现的,也不知道为什么要这么做。

你的方法感觉不正确,我把这个函数申明为Chain和ChainNode类的友元函数,问题得到解决。原来的问题是我友元的友元,不是我的友元。

yy57 发表于 2014-10-23 18:52:13

硫酸亚铜 发表于 2014-10-23 18:45
你的方法感觉不正确,我把这个函数申明为Chain和ChainNode类的友元函数,问题得到解决。原来的问题是我友 ...

我没仔细看。。。楼主抱歉。。。

yy57 发表于 2014-10-23 18:53:06

硫酸亚铜 发表于 2014-10-23 18:45
你的方法感觉不正确,我把这个函数申明为Chain和ChainNode类的友元函数,问题得到解决。原来的问题是我友 ...

你修正后的代码是怎么样的,贴出来瞧瞧。

硫酸亚铜 发表于 2014-10-24 09:43:21

yy57 发表于 2014-10-23 18:53
你修正后的代码是怎么样的,贴出来瞧瞧。

#include <iostream>
using namespace std;
template <class T>
class Chain;
template <class T>
class ChainNode
{
        friend class Chain<T>;
        friend ostream& operator<< <T>(ostream& out, const Chain<T>& x);
private:
        T data;
        ChainNode<T>* link = 0;
};

template <class T>
class Chain
{
        friend ostream& operator<< <T>(ostream& out, const Chain<T>& x);
public:
        Chain(){ first = 0; }
        Chain<T>& InsertChange(int k);
private:
        ChainNode<T>* first;//指向第一个节点的指针
};

template <class T>
ostream& operator<< (ostream& out, const Chain<T>& x);
int main()
{
        Chain<int> test;
        for (int i = 0; i < 10; i++)
        {
                test.InsertChange(i);
        }
        cout << test; //此处报错!!!
        return 0;

}
template <class T>
Chain<T>& Chain<T>::InsertChange(int k)
{
        ChainNode<T>* p = new ChainNode<T>;
        ChainNode<T>* q;
        p->data = k;
        if (!first)
        {
                first = p;
        }
        else
        {
                q = first;
                while (q->link)
                {
                        q = q->link;
                }
                q->link = p;
        }
        return *this;
}

//重载<<
template<class T>
ostream& operator<< (ostream& out, const Chain<T>& x)
{
        ChainNode<T>* p;
        for (p = x.first; p; p = p->link)
        {
                out << p->data << endl;
        }
        return out;
}

yy57 发表于 2014-10-24 10:22:20

硫酸亚铜 发表于 2014-10-24 09:43


学习了~               

硫酸亚铜 发表于 2014-10-24 14:23:53

yy57 发表于 2014-10-24 10:22
学习了~

:handshake

大个的糖果 发表于 2014-10-30 17:55:48

页: [1]
查看完整版本: 请教各位大虾重载<<运算符的问题