关于mem_fun,MSDN上的例子居然有编译错误!!!

fengfeng2003 2003-09-11 04:00:42
#include "iostream"
#include "vector"
#include "algorithm"
#include "functional"
using namespace std;

struct B {
virtual void print() = 0;
};

struct D1 : public B {
void print() { cout << "I'm a D1" << endl; }
};

struct D2 : public B {
void print() { cout << "I'm a D2" << endl; }
};

void main()
{
vector<B*> V;

V.push_back(new D1);
V.push_back(new D2);
V.push_back(new D2);
V.push_back(new D1);

for_each(V.begin(), V.end(), mem_fun(&B::print));
}

--------------------Configuration: ThermalSys - Win32 Debug--------------------
Compiling...
ThermalSys.cpp
d:\microsoft visual studio\vc98\include\functional(233) : error C2562: '()' : 'void' function returning a value
d:\microsoft visual studio\vc98\include\functional(232) : see declaration of '()'
d:\microsoft visual studio\vc98\include\functional(233) : while compiling class-template member function 'void __thiscall std::mem_fun_t<void,class Heater>::operator ()(class Heater *) const'
Error executing cl.exe.

ThermalSys.obj - 1 error(s), 0 warning(s)

改怎么解决这个问题啊?
如果把void改为int编译通过。
另外我想问一下像在这个程序中,用new分配空间,最后不用delete吗?
难道是vector自动delete,不解???
...全文
69 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
fancy_kevin 2003-09-13
  • 打赏
  • 举报
回复
纯粹编译器的问题,另外,new出来的对象在程序结束时不会被析构的,这里只是个例子。
fengfeng2003 2003-09-11
  • 打赏
  • 举报
回复
不明白,到底怎么回事
ZHENG017 2003-09-11
  • 打赏
  • 举报
回复
#include "iostream"
#include "vector"
#include "algorithm"
#include "functional"
using namespace std;

struct B {
virtual bool print() = 0;
};

struct D1 : public B {
bool print() { cout << "I'm a D1" << endl; return 0;}
};

struct D2 : public B {
bool print() { cout << "I'm a D2" << endl; return 0;};
};

void main()
{
vector<B*> V;

V.push_back(new D1);
V.push_back(new D2);
V.push_back(new D2);
V.push_back(new D1);

for_each(V.begin(), V.end(), mem_fun<bool,B> (&B::print) );
}
fengfeng2003 2003-09-11
  • 打赏
  • 举报
回复
用的vc6啊
sevecol 2003-09-11
  • 打赏
  • 举报
回复
你是用什么编译器?

VC7.1没问题呀。

24,854

社区成员

发帖
与我相关
我的任务
社区描述
C/C++ 工具平台和程序库
社区管理员
  • 工具平台和程序库社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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