。。关于指向函数的指针C++PRIMER

touchwhat 2009-03-05 12:52:07
代码不完整的。。我按C++PRIMER。。就想按书上输出pf1。。大家能帮我补下吗。。
#include<iostream>
using namespace std;
int main()
{
typedef bool (*cmpFcn)(const string &str1,const string &str2);
bool lengthCompare(const string &str3,const string &str4);
string::size_type sumLength(const string &str5,const string &str6);
bool cstringCompare(char*,char*);
cout<<"Hello world!"<<endl;
cmpFcn pf=lengthCompare;
lengthCompare("hi","bye");
pf('hi","bye");
(*pf)("hi","bye");
cout<<pf1<<endl;
cout<<*pf1<<endl;
}
bool lengthCompare(const string &str3,const string &str4)
{
if(str3.size()<str4.size())
return 1;
else
return 0;
}
string::size_type sumLength(const string &str5,const string &str6)
{
if(str5.size()>str6.size())
return 1;
else
return 0;
}
...全文
99 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
pengzhixi 2009-03-05
  • 打赏
  • 举报
回复
string::size_type sumLength(const string &str5,const string &str6)
{
if(str5.size()>str6.size())
return 1;
else
return 0;
}
你这个函数写错了吧,再者 你输出函数指针干啥.
touchwhat 2009-03-05
  • 打赏
  • 举报
回复
好了。。
#include<iostream>
#include<string>
using namespace std;
int main()
{
typedef bool (*cmpFcn)(const string &str1,const string &str2);
bool lengthCompare(const string &str3,const string &str4);
bool cstringCompare(char*,char*);
cout<<"Hello world!"<<endl;
cmpFcn pf=lengthCompare;
lengthCompare("hi","bye");
bool a=pf("hi","bye");
bool b=(*pf)("hi","bye");
cout<<a<<endl;
cout<<b<<endl;
}
bool lengthCompare(const string &str3,const string &str4)
{
if(str3.size()<str4.size())
return 1;
else
return 0;
}


好了。。原来pf("hi","bye");是个函数不是初始化。。。。我定义了一个逻辑值来赋值就好了。。
MooreChen 2009-03-05
  • 打赏
  • 举报
回复
1. #include <string>
2. pf('hi","bye") -> pf("hi","bye")

pf1是在哪里定义的,pf?

如果是pf的话,pf 和 *pf 的值是一样的,都是指向函数的地址,
pf("hi","bye")和 (*pf)("hi","bye") 也是一样的,都会调用lengthCompare("hi", "byt");
touchwhat 2009-03-05
  • 打赏
  • 举报
回复
#include<iostream>
using namespace std;
int main()
{
typedef bool (*cmpFcn)(const string &str1,const string &str2);
bool lengthCompare(const string &str3,const string &str4);
bool cstringCompare(char*,char*);
cout<<"Hello world!"<<endl;
cmpFcn pf=lengthCompare;
lengthCompare("hi","bye");
pf('hi","bye");
(*pf)("hi","bye");
cout<<pf<<endl;
cout<<*pf<<endl;
}
bool lengthCompare(const string &str3,const string &str4)
{
if(str3.size()<str4.size())
return 1;
else
return 0;
}


截了一点。。我是想应该可以pf指针输出0的。。
Qlaiaqu 2009-03-05
  • 打赏
  • 举报
回复
pf('hi","bye");
看见这里没有。单引号。
pf1没有定义啊,是要输出pf吧你
flameearth 2009-03-05
  • 打赏
  • 举报
回复
cout<<pf1<<endl;
cout<<*pf1<<endl;
因该输出调用指针函数吧 pf1(....)
rgmlkthh 2009-03-05
  • 打赏
  • 举报
回复
题没有完全吧,pf1哪里定义的都没有呢。

65,211

社区成员

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

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