effective C++ item 35 例程编译不通过!求指教!

Full_Speed_Turbo 2014-01-24 05:50:26
源代码如下:
#include <iostream>
#include <iomanip>
//#include <memory>
#include <functional>
class game_character;
int default_health_calc(const game_character& gc);
class game_character{
public:
typedef std::tr1::function<int(const game_character&)> health_calc_func;
explicit game_character(health_calc_func hcf = default_health_calc)
:health_func(hcf)
{}
int health_value()const
{
return health_func(*this);
}
private:
health_calc_func health_func;
};
short calc_health(const game_character&)
{
std::cout << "short calc_health() " << std::endl;
return 0;
}
struct health_calculator{
int operator()(const game_character&)const
{
std::cout << "the struct health_calculator int operator()" << std::endl;
return 0;
}
};
class game_level{
public:
float health(const game_character&) const
{
std::cout << "the class game_level float health()" << std::endl;
return 0;
}
};
class evil_bad_guy :public game_character{
public:
explicit evil_bad_guy(health_calc_func hcf = default_health_calc)
:game_character(hcf)
{}

};
class eye_candy_character :public game_character{
public:
explicit eye_candy_character(health_calc_func hcf = default_health_calc)
:game_character(hcf)
{}
};

int main()
{
evil_bad_guy ebg1(calc_health);//short calc_health()
ebg1.health_value();
eye_candy_character ecc1(health_calculator());
//ecc1.health_value();//有错误!the struct health_calculator int operator()
game_level current_level;
evil_bad_guy ebg2(std::tr1::bind(&game_level::health, current_level, std::tr1::placeholders::_1));
ebg2.health_value();//the class game_level float health()
}

ecc1.health_value();这一行有错误,请问是调用格式不对吗?
请各位不吝指教,谢谢!
...全文
187 3 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
xiao_hao_2014 2014-01-29
  • 打赏
  • 举报
回复
just want to close it !
Full_Speed_Turbo 2014-01-24
  • 打赏
  • 举报
回复
引用 1 楼 hello_shadow 的回复:
int health_value()const //这里少一个空格     {         return health_func(*this); //const函数内部职能调用const函数。     } 你应该把错误提示信息贴出来,不然别人不好帮你。
错误 1 error C2228: “.health_value”的左边必须有类/结构/联合 c:\users\rong\documents\visual studio 2013\projects\effective_cpp_55\effective_cpp_55\test_item_35_3.cpp 59 2 IntelliSense: 表达式必须包含类类型 c:\Users\rong\Documents\Visual Studio 2013\Projects\Effective_cpp_55\Effective_cpp_55\test_item_35_3.cpp 59
梦之安魂曲 2014-01-24
  • 打赏
  • 举报
回复
int health_value()const //这里少一个空格     {         return health_func(*this); //const函数内部职能调用const函数。     } 你应该把错误提示信息贴出来,不然别人不好帮你。

33,321

社区成员

发帖
与我相关
我的任务
社区描述
C/C++ 新手乐园
社区管理员
  • 新手乐园社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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