[求救]报错:没有找到接受“void”类型的右操作数的运算符(或没有可接受的转换)

achang21 2010-08-25 09:23:58
我是c++初学者,写了一段学习 函数指针的代码,运行报错,希望大家能帮我,非常感谢。
代码如下:

#include "stdafx.h"


double bery(int);
double pum(int);
void estimate(int lines,double (*pf)(int));

int _tmain(int argc, _TCHAR* argv[])
{
using namespace std;
int a;

cin>>a;
cout<<estimate(a,bery)<<endl;
cout<<estimate(a,pum)<<endl;

return 0;
}



double bery(int a)
{
return a*5;
}

double pum(int a)
{
return a+5;
}

void estimate(int lines,double(*pf)(int))
{
using namespace std;
cout<<lines<<"will take"<<endl;
cout<<(*pf)(lines)<<" check!"<<endl;

}


说明:iostream 文件已经在stdafx.h包含。

运行报错error C2679: 二进制“<<”: 没有找到接受“void”类型的右操作数的运算符(或没有可接受的转换)
大家帮我看下。
...全文
608 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
编程实战营 2012-12-27
  • 打赏
  • 举报
回复
今天我又遇到了这个问题,cout<<void这种用法是不对的
cjingzm 2010-08-25
  • 打赏
  • 举报
回复
把你的这段代码
cout<<estimate(a,bery)<<endl;
cout<<estimate(a,pum)<<endl;
改为:
estimate(a,bery);
cout<<endl;
estimate(a,pum);
cout<<endl;
就可以了。
你自己能看出你哪里出错了吗? 你原来的cout输出的是函数estimate的返回值,而estimate的返回值是void,当然会报错了。
achang21 2010-08-25
  • 打赏
  • 举报
回复
非常感激大家详细的解答,确实是 大家说的那样!再次感谢,后边多向大家学习^_^
bluejays 2010-08-25
  • 打赏
  • 举报
回复
可以让estimate函数返回某个类型,然后用cout输出这个返回值,但是好像没什么必要。
既然estimate函数里面已经输出了,main里面就直接调用他一下就可以了,不必再cout了
bluejays 2010-08-25
  • 打赏
  • 举报
回复
estimate函数返回void,而cout<<void是不行的。
bluejays 2010-08-25
  • 打赏
  • 举报
回复
cin>>a;
estimate(a,bery);
estimate(a,pum);
return 0;

64,654

社区成员

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

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