BBYR Achieve
返回信息流
这是一条镜像帖。来源:北邮人论坛 / cpp / #7287同步于 2008/5/23
该镜像源已超过 30 天没有更新,可能在源站已被删除。
CPP机器人发帖

[合集] [求助]泛型算法sort()的问题……

Xer
2008/5/23镜像同步0 回复
☆─────────────────────────────────────☆ Vampire (黑暗祭礼) 于 (Sat May 17 20:09:08 2008) 提到: 定义了一个类 class A { public: typdef struct { long long low,high; } node; //declare structure 'node' node a[1000]; //array of type 'node' long long x,y; //declare 'x', 'y' as long long int bool cmp(node x,node y) { //comparing function used by 'sort()' algorithm return x.low<y.low; } vector<string> whichSums(string target) { //function //.....do something... sort(a,a+n,cmp); //call sort(), where 'n' is the current size of the array 'a' //.....do something and return } }; Dev-C++编译报错: 调用sort()算法的那一行: argument of type `bool (Gauss::)(Gauss::node, Gauss::node)' does not match `bool (Gauss::*)(Gauss::node, Gauss::node)' ... 后面还有很多,都是报的那一行有问题 我把A类定义里面的cmp函数以及之前的所有变量定义都拿到class外面去作为全局就过编译了…… 请问一下这是什么原因啊??谢谢~~! ☆─────────────────────────────────────☆ vivin (阿诺) 于 (Sun May 18 12:40:11 2008) 提到: 可以把 bool cmp(node x,node y) { //comparing function used by 'sort()' algorithm return x.low<y.low; } 声明为static的,这样相当于声明为全局的 或者在使用sort的时候加上mem_fun适配器 ☆─────────────────────────────────────☆ vivin (阿诺) 于 (Sun May 18 12:44:19 2008) 提到: 这里好像不能使用mem_fun,除非那个cmp是在node里面定义的。 【 在 vivin 的大作中提到: 】 : 可以把 : bool cmp(node x,node y) { //comparing function used by 'sort()' algorithm : return x.low<y.low; : ................... ☆─────────────────────────────────────☆ Grape (葡萄|热烈庆祝自己升级为大MJ) 于 (Sun May 18 14:51:14 2008) 提到: 成员函数指针与非成员函数指针有巨大的区别 成员函数指针还包括有类的信息 有三种解决方案 LS说的对 上传一个文档 [upload=1][/upload] 【 在 Vampire 的大作中提到: 】 : 定义了一个类 : class A { : public: : ................... ☆─────────────────────────────────────☆ Vampire (黑暗祭礼) 于 (Sun May 18 17:04:53 2008) 提到: 先谢过几位~ 为什么那个cmp放在class里面就报错呢?不能在sort里面使用一个类里面定义的函数么?……mem_fun是什么…… ☆─────────────────────────────────────☆ kind (佩佩) 于 (Sun May 18 18:28:01 2008) 提到: mem_fun是使用类的某个成员函数对类操作。 函数对象里常用。 【 在 Vampire (黑暗祭礼) 的大作中提到: 】 : 先谢过几位~ : 为什么那个cmp放在class里面就报错呢?不能在sort里面使用一个类里面定义的函数么?……mem_fun是什么…… ☆─────────────────────────────────────☆ kind (佩佩) 于 (Sun May 18 18:31:08 2008) 提到: 你可以看看TC++PL第3章,有使用mem_fun的例子。 【 在 Vampire (黑暗祭礼) 的大作中提到: 】 : 先谢过几位~ : 为什么那个cmp放在class里面就报错呢?不能在sort里面使用一个类里面定义的函数么?……mem_fun是什么…… ☆─────────────────────────────────────☆ Grape (葡萄|热烈庆祝自己升级为大MJ) 于 (Sun May 18 20:41:53 2008) 提到: 你能仔细看看我的回帖和附的文章再提问题么? 【 在 Vampire 的大作中提到: 】 : 先谢过几位~ : 为什么那个cmp放在class里面就报错呢?不能在sort里面使用一个类里面定义的函数么?……mem_fun是什么…… ☆─────────────────────────────────────☆ FadeToBlack (Forever MetallicA) 于 (Mon May 19 19:00:21 2008) 提到: 正好前几天也碰见同样问题,加了个static过了,但不知道为什么。thx~ ☆─────────────────────────────────────☆ pmps (pmps) 于 (Tue May 20 10:20:02 2008) 提到: 类的非静态成员函数有一个隐藏的参数this,而静态函数和全局函数就没有,所以两种函数指针不能互转 ☆─────────────────────────────────────☆ Vampire (黑暗祭礼) 于 (Tue May 20 15:52:42 2008) 提到: 了解……多谢~ 【 在 pmps 的大作中提到: 】 : 类的非静态成员函数有一个隐藏的参数this,而静态函数和全局函数就没有,所以两种函数指针不能互转
订阅后,新回复会通过你的通知中心匿名送达。
0 条回复
暂无回复 · 你可以订阅本帖等待新回复。