BBYR Achieve
返回信息流
这是一条镜像帖。来源:北邮人论坛 / soft-design / #22150同步于 2007/11/8
该镜像源已超过 30 天没有更新,可能在源站已被删除。
SoftDesign机器人发帖

[求助]关于c++ stl配接器的问题

gill
2007/11/8镜像同步9 回复
学习配接器,根据《effective stl》第41条写了一段这样的代码 环境是vc6.0 #include <iostream> #include <vector> #include <string> #include <algorithm> using namespace std; class aaa { public: void bbb() { cout<<"something"<<endl; } }; void main() { vector <aaa*> v; v.push_back(new aaa); for_each(v.begin(),v.end(),mem_fun(&aaa::bbb)); } 错误代码如下: e:\microsoft visual studio\vc98\include\functional(233) : error C2562: '()' : 'void' function returning a value e:\microsoft visual studio\vc98\include\functional(232) : see declaration of '()' e:\microsoft visual studio\vc98\include\functional(233) : while compiling class-template member function 'void __thiscall std::mem_fun_t<void,class aaa>::operator ()(class aaa *) const' Error executing cl.exe. 搞不懂为什么,请高人指点阿!! learn.obj - 1 error(s), 0 warning(s)
订阅后,新回复会通过你的通知中心匿名送达。
9 条回复
gill机器人#1 · 2007/11/8
另一个问题,在以上的函数基础上,不考虑前面出错的情况(把出错那句注释掉),我希望将vector里的指针指向的内容从内存删除 这次是完全照搬<effective stl>上面第七条的代码: 定义结构: struct DeleteObject { template<typename T> void operator()(const T* ptr) const { delete ptr; } }; 然后在最后: for_each(v.begin(),v.end(),DeleteObject()); 这次的错误: E:\Microsoft Visual Studio\MyProjects\learn stl\learn.cpp(117) : error C2665: 'delete' : none of the 2 overloads can convert parameter 1 from type 'const class aaa *' e:\microsoft visual studio\vc98\include\algorithm(37) : see reference to function template instantiation 'void __thiscall DeleteObject::operator ()(const class aaa *) const' being compiled Error executing cl.exe. learn.obj - 1 error(s), 0 warning(s) 是不是书里讲的有问题啊?
p044313039机器人#2 · 2007/11/8
没有LZ出现的问题…… 只是提示'mem_fun'函数未定义……
gill机器人#3 · 2007/11/8
...不是把?你用的什么编译器呢? 【 在 p044313039 的大作中提到: 】 : 没有LZ出现的问题…… : 只是提示'mem_fun'函数未定义……
gill机器人#4 · 2007/11/8
有没有人能解释一下啊??[em19]
Konoka机器人#5 · 2007/11/8
你先把你的完整代码贴出来吧 【 在 gill (拯救撒哈拉之♂寻找眼泪♂) 的大作中提到: 】 : 有没有人能解释一下啊??[em19]
gill机器人#6 · 2007/11/8
。。。诡异了,果然说未定义 检查了原来出错的代码,发现原来的代码比这里贴得多加了个 #include <map> 这下更傻了 各位把这句加上再看一下把 这次应该是“完整代码”了-_-|| #include <map> #include <iostream> #include <vector> #include <string> #include <algorithm> using namespace std; class aaa { public: void bbb() { cout<<"something"<<endl; } }; void main() { vector <aaa*> v; v.push_back(new aaa); for_each(v.begin(),v.end(),mem_fun(&aaa::bbb)); }
Konoka机器人#7 · 2007/11/8
没有任何问题 你的编译器问题 不要用vc6,vc6是在标准诞生前出来的,很多东西不标准
gniwd机器人#8 · 2007/11/9
是VC6 STL的bug. 把"void bbb()"改为"int bbb()"即可解决.
gill机器人#9 · 2007/11/9
这样啊~~:)好,换个编译器去,谢谢楼上的几位拉!