bind2nd( mem_fun(&AClass::AFun)的错误

yaoyansi 2008-12-26 03:29:00
在使用bind2nd( mem_fun <bool,Test,const string& >(&Test::compareByStringRef)时,提示
1>VC_INCLUDE\functional(312) : error C2529: '_Right' : reference to reference is illegal

对于mem_fun,mem_fun1, mem_fun_ref....系列的函数不是很了解,在msdn上没有找到哪个适用于"成员函数里带'引用参数' "的例子.
所以也不知道应该用哪一个.
请高手指点一下.
谢谢

以下是code



#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <functional>
using namespace std;
/*
typedef (const string&) constStringRef;
typedef (const string ) constString;
//typedef string& StringRef;
*/
///------------------------------------------------
class Test
{
public:
Test(int a, const string& b)
:_a(a), _b(b){ }
~Test() {}
bool compareByInt( const int a){
return a == _a;
}
bool compareByString(const string b){
return b == _b;
}
bool compareByStringRef(const string& b){ <-------- const string& b
return b == _b;
}
int _a;
string _b;
void print(){
cout << "<" << _a << "," << _b << ">, " ;
}
};
void print();
///------------------------------------------------
vector < Test > vt;
///------------------------------------------------
int main()
{
vt.push_back(Test(1, "A"));
vt.push_back(Test(2, "B"));
vt.push_back(Test(3, "C"));
vt.push_back(Test(4, "D"));
print();

//--运行OK------------------------------------
vt.erase( remove_if(vt.begin(), vt.end(),
bind2nd( mem_fun1_ref<bool,Test, const int>(&Test::compareByInt)
, 2 )
),
vt.end());
print();


//--运行OK------------------------------------
vt.erase( remove_if(vt.begin(), vt.end(),
bind2nd( mem_fun1_ref<bool,Test, const string>(&Test::compareByString)
, "D" )
),
vt.end());
print();

/*---编译错误-----------------------------------
1>...\....\functional(312) : error C2529: '_Right' : reference to reference is illegal
我想使用compareByStringRef(const string& b),因为这比compareByString(const string b)效率高,
可是无法编译,怎么办? 难道是应该用mem_funX_XXX系列中的其他几个?
*/
vt.erase( remove_if(vt.begin(), vt.end(),
bind2nd( mem_fun<bool,Test,const string& >(&Test::compareByStringRef)
, "A" )
),
vt.end());
print();

return 1;
}
///------------------------------------------------
void print()
{
vector < Test > ::iterator i = vt.begin();
vector < Test > ::iterator e = vt.end();
for(; i!=e; ++i){
i->print();
}
cout << endl;
}


...全文
140 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
就呆在云上 2008-12-26
  • 打赏
  • 举报
回复

问题出在你的函数的使用啦,不要用mem——fun函数
他使用的是指针,mem_fun_ref才是使用引用的使用的,_ref的意思就在这里:

#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <functional>
using namespace std;
/*
typedef (const string&) constStringRef;
typedef (const string ) constString;
//typedef string& StringRef;
*/
///------------------------------------------------
class Test
{
public:
Test(int a, const string& b)
:_a(a), _b(b){ }
~Test() {}
bool compareByInt( const int a){
return a == _a;
}
bool compareByString(const string b){
return b == _b;
}
bool compareByStringRef(const string& b){ //<-------- const string& b
return b == _b;
}
int _a;
string _b;
void print(){
cout << "<" << _a << "," << _b << ">, " ;
}
};
void print();
///------------------------------------------------
vector < Test > vt;
///------------------------------------------------
int main()
{
vt.push_back(Test(1, "A"));
vt.push_back(Test(2, "B"));
vt.push_back(Test(3, "C"));
vt.push_back(Test(4, "D"));
print();

//--运行OK------------------------------------
vt.erase( remove_if(vt.begin(), vt.end(),
bind2nd(
mem_fun1_ref<bool,Test, const int>(&Test::compareByInt)
, 2 )
),
vt.end());
print();


//--运行OK------------------------------------
vt.erase( remove_if(vt.begin(), vt.end(),
bind2nd( mem_fun1_ref<bool,Test, const string>(&Test::compareByString)
, "D" )
),
vt.end());
print();


vt.erase( remove_if(vt.begin(),vt.end(),
bind2nd( mem_fun_ref<bool,Test,const string& >(&Test::compareByStringRef)
, "A" ) ), vt.end());
print();

return 1;
}
///------------------------------------------------
void print()
{
vector < Test > ::iterator i = vt.begin();
vector < Test > ::iterator e = vt.end();
for(; i!=e; ++i){
i->print();
}
cout << endl;
}
yaoyansi 2008-12-26
  • 打赏
  • 举报
回复
非常感谢!
yutaooo 2008-12-26
  • 打赏
  • 举报
回复

哦 , 错了。是 20%中的20%。 反正占有效代码的比例太小了。
yutaooo 2008-12-26
  • 打赏
  • 举报
回复

这个主要就是诸如如下的声明造成的。



int &&x; // 引用的引用不合法,必须想办法去掉一个&



基本上我觉得你这个问题无解了, 去掉一个 & 的工作一般在库设计这一端完成。一般是通过元函数,判断是否引用,然后采用合适的类型(引用而非引用的引用)。

别执着这里了。这种都是 80%中的80%,实际意义太小了。不值得的。
xkyx_cn 2008-12-26
  • 打赏
  • 举报
回复
问题并不出在mem_fun系列函数的使用上:

// It's ok
mem_fun_ref<bool,Test,const string& >(&Test::compareByStringRef)(Test(11, "A"), "A");


问题出在STL的bind2nd的实现,下面用boost的 bind 解决了:

vt.erase( remove_if(vt.begin(), vt.end(),
boost::bind( &Test::compareByStringRef, _1, "A" )),
vt.end());
yaoyansi 2008-12-26
  • 打赏
  • 举报
回复
顶一下,求解答.

谢谢回复.
taodm 2008-12-26
  • 打赏
  • 举报
回复
找本《stl源码剖析》看mem_族的章节。
stl只靠msdn是很不够的。

65,211

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧