免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 2001 | 回复: 9
打印 上一主题 下一主题

请教一个友元函数的问题,谢谢! [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2010-03-24 23:56 |只看该作者 |倒序浏览
代码如下:
template <class Type>
class IteratorDouList
{
private:
        DouNode<Type>* cur;
public:
        IteratorDouList():cur(NULL){};
        IteratorDouList(DouNode<Type>* curNode):cur(curNode){};

        //friend Type operator++(IteratorDouList<Type>& itDouList){return itDouList.GetCur()->GetData();};
                 friend Type operator++(IteratorDouList<Type>& itDouList);

        void operator=(DouNode<Type>& pointNode);
        DouNode<Type>* GetCur();
};

template <class Type>
Type operator++(IteratorDouList<Type>& itDouList)
{
        return ......
}

上述代码在用的时候,如:
IteratorDouList<int> test;
.....
++test;
...
报错如下:
错误        1        error LNK2019: 无法解析的外部符号 "int __cdecl operator++(class IteratorDouList<int> &,int)" (??E@YAHAAV?$IteratorDouList@H@@H@Z),该符号在函数 _main 中被引用        Test_Mian.obj       
错误        2        fatal error LNK1120: 1 个无法解析的外部命令        H:\C++\Template_class\Debug\Template_class.exe       

但是如果把“++”的实现移植到类的内部(如注释掉的部分)就对了。
新手,不理解,请大家多指点。

论坛徽章:
0
2 [报告]
发表于 2010-03-25 10:24 |只看该作者
回复 1# tutern


    1        error LNK2019: 无法解析的外部符号 "int __cdecl operator++(class IteratorDouList<int> &,int)" (??E@YAHAAV?$IteratorDouList@H@@H@Z),该符号在函数 _main 中被

论坛徽章:
0
3 [报告]
发表于 2010-03-25 10:34 |只看该作者
template <class Type>
Type operator++(IteratorDouList<Type>& itDouList)
{
        return ......
}

这个函数定义好像错了,不明白这是成员函数,还是全局函数?

论坛徽章:
0
4 [报告]
发表于 2010-03-25 11:28 |只看该作者
friend Type operator++(IteratorDouList<Type>& itDouList);
这种声明是定义test++
修改为
friend Type operator++();

论坛徽章:
0
5 [报告]
发表于 2010-03-25 17:48 |只看该作者
本帖最后由 咖喱包 于 2010-03-25 17:50 编辑

"friend Type operator++(IteratorDouList<Type>& itDouList);"

友元的话,应该是这样定义,只不是应该返回friend IteratorDouList<Type>& ??

论坛徽章:
0
6 [报告]
发表于 2010-03-25 21:11 |只看该作者
回复 2# rain_fish


呵呵。。。你看的还真仔细,这是个后缀的函数的错误,昨天晚了点,我搞来搞去乱了,但是错误该是和前缀的一样的吧

论坛徽章:
0
7 [报告]
发表于 2010-03-25 21:17 |只看该作者
回复 4# chary8088

刚试了一下,报错
错误        1        error C2809: “operator ++”没有形参        h:\c++\template_class\template_class\IteratorDouList.h        19

论坛徽章:
0
8 [报告]
发表于 2010-03-25 21:30 |只看该作者
回复 5# 咖喱包

其实我想实现的是一个游标类,这个函数是想返回节点的数据值,不是节点指着,也不是游标类指针。

论坛徽章:
0
9 [报告]
发表于 2010-04-02 16:06 |只看该作者
。。。

论坛徽章:
0
10 [报告]
发表于 2010-04-02 20:46 |只看该作者
有些编译器只允许友元在类体内定义~?
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP