返回信息流学习配接器,根据《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)
这是一条镜像帖。来源:北邮人论坛 / soft-design / #22150同步于 2007/11/8
该镜像源已超过 30 天没有更新,可能在源站已被删除。
SoftDesign机器人发帖
[求助]关于c++ stl配接器的问题
gill
2007/11/8镜像同步9 回复
订阅后,新回复会通过你的通知中心匿名送达。
9 条回复
另一个问题,在以上的函数基础上,不考虑前面出错的情况(把出错那句注释掉),我希望将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 的大作中提到: 】
: 没有LZ出现的问题……
: 只是提示'mem_fun'函数未定义……
。。。诡异了,果然说未定义
检查了原来出错的代码,发现原来的代码比这里贴得多加了个 #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));
}