免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 6943 | 回复: 4
打印 上一主题 下一主题

类成员函数指针的问题,编译不通过,大家帮忙看下。 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2007-11-14 16:14 |只看该作者 |倒序浏览
代码如下:
  1 #ifndef _USERLOGIN_PROTOCOL_H
  2 #       define _USERLOGIN_PROTOCOL_H
  3
  4 #include "protocols.h"
  5
  6 //typedef int (*pro_Action)(int);

  7
  8 class protocolController
  9 {
 10 public:
 11         protocolController();
 12 private:
 13         //pro_Action action[30];

 14         int (protocolController::*action[30])(int) const;
 15
 16         int action_NULL(int signal) const;
 17         int action_ServerStatus(int signal) const;
 18         int action_LoginUsername(int signal) const;
 19         int action_LoginPassword(int signal) const;
 20         int action_RegisterUsername(int signal) const;
 21         int action_RegisterPassword(int signal) const;
 22         int action_RegisterEmail(int signal) const;
 23 };
 24
 25 typedef int (protocolController::*pro_Action)(int);
 26 #endif


  1 #include "protocolController.h"
  2 #include "protocols.h"
  3 #include <string.h>
  4
  5 protocolController::protocolController()
  6 {
  7         for (int i = 0; i < 30; i++)
  8         {
  9                 action[ i ] = &action_NULL;
10         }
11         action[C_BEDEFINE_0]        = &action_NULL;
12         action[C_SERVER_STATUS]     = &action_ServerStatus;
13         action[C_LOGIN_USERNAME]    = &action_LoginUsername;
14         action[C_LOGIN_PASSWORD]    = &action_LoginPassword;
15         action[C_REGISTER_USERNAME] = &action_RegisterUsername;
16         action[C_REGISTER_PASSWORD] = &action_RegisterPassword;
17         action[C_REGISTER_EMAIL]    = &action_RegisterEmail;
18 }


编译结果如下:
  1. $ g++ -c protocolController.cpp -o protocolController.o
  2. protocolController.cpp: In constructor 'protocolController::protocolController()':
  3. protocolController.cpp:9: error: ISO C++ forbids taking the address of an unqualified or parenthesized non-static member function to form a pointer to member function.  Say '&protocolController::action_NULL'
  4. protocolController.cpp:11: error: ISO C++ forbids taking the address of an unqualified or parenthesized non-static member function to form a pointer to member function.  Say '&protocolController::action_NULL'
  5. protocolController.cpp:12: error: ISO C++ forbids taking the address of an unqualified or parenthesized non-static member function to form a pointer to member function.  Say '&protocolController::action_ServerStatus'
  6. protocolController.cpp:13: error: ISO C++ forbids taking the address of an unqualified or parenthesized non-static member function to form a pointer to member function.  Say '&protocolController::action_LoginUsername'
  7. protocolController.cpp:14: error: ISO C++ forbids taking the address of an unqualified or parenthesized non-static member function to form a pointer to member function.  Say '&protocolController::action_LoginPassword'
  8. protocolController.cpp:15: error: ISO C++ forbids taking the address of an unqualified or parenthesized non-static member function to form a pointer to member function.  Say '&protocolController::action_RegisterUsername'
  9. protocolController.cpp:16: error: ISO C++ forbids taking the address of an unqualified or parenthesized non-static member function to form a pointer to member function.  Say '&protocolController::action_RegisterPassword'
  10. protocolController.cpp:17: error: ISO C++ forbids taking the address of an unqualified or parenthesized non-static member function to form a pointer to member function.  Say '&protocolController::action_RegisterEmail'
  11. $
复制代码

[ 本帖最后由 yxmab 于 2007-11-15 09:05 编辑 ]

论坛徽章:
0
2 [报告]
发表于 2007-11-14 18:31 |只看该作者
>>action = &action_NULL;

你觉得action是个什么东东???

论坛徽章:
0
3 [报告]
发表于 2007-11-14 20:06 |只看该作者
是我初始化的一个函数指针,准备来调用各个函数用的,循环里的变量在粘贴时出错把下标丢了,不过,下面的都是对的。

论坛徽章:
0
4 [报告]
发表于 2007-11-14 20:12 |只看该作者
&protocolController::action_NULL

论坛徽章:
0
5 [报告]
发表于 2007-11-15 14:18 |只看该作者

原来是我画蛇添足了

我竟然对&protocolController::action_NULL加了括号,成了&(protocolController::action_NULL),结果怎么也成功不了,现在成功了
在一成员函数里用“(this->*action[x])(strData);”语句调用成功。
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP