LinuxSir.cn,穿越时空的Linuxsir!

 找回密码
 注册
搜索
热搜: shell linux mysql
查看: 1365|回复: 4

类成员函数指针的问题,编译不通过,大家帮忙看下。(已解决)

[复制链接]
发表于 2007-11-14 15:22:12 | 显示全部楼层 |阅读模式
源文件如下:

  1.   1 #ifndef _USERLOGIN_PROTOCOL_H
  2.   2 #       define _USERLOGIN_PROTOCOL_H
  3.   3
  4.   4 #include "protocols.h"
  5.   5
  6.   6 //typedef int (*pro_Action)(int);
  7.   7
  8.   8 class protocolController
  9.   9 {
  10. 10 public:
  11. 11         protocolController();
  12. 12 private:
  13. 13         //pro_Action action[30];
  14. 14         int (protocolController::*action[30])(int) const;
  15. 15
  16. 16         int action_NULL(int signal) const;
  17. 17         int action_ServerStatus(int signal) const;
  18. 18         int action_LoginUsername(int signal) const;
  19. 19         int action_LoginPassword(int signal) const;
  20. 20         int action_RegisterUsername(int signal) const;
  21. 21         int action_RegisterPassword(int signal) const;
  22. 22         int action_RegisterEmail(int signal) const;
  23. 23 };
  24. 24
  25. 25 typedef int (protocolController::*pro_Action)(int);
  26. 26 #endif
复制代码


  1.   1 #include "protocolController.h"
  2.   2 #include "protocols.h"
  3.   3 #include <string.h>
  4.   4
  5.   5 protocolController::protocolController()
  6.   6 {
  7.   7         for (int i = 0; i < 30; i++)
  8.   8         {
  9.   9                 action[i] = &(protocolController::action_NULL);
  10. 10         }
  11. 11         action[C_BEDEFINE_0]        = &action_NULL;
  12. 12         action[C_SERVER_STATUS]     = &action_ServerStatus;
  13. 13         action[C_LOGIN_USERNAME]    = &action_LoginUsername;
  14. 14         action[C_LOGIN_PASSWORD]    = &action_LoginPassword;
  15. 15         action[C_REGISTER_USERNAME] = &action_RegisterUsername;
  16. 16         action[C_REGISTER_PASSWORD] = &action_RegisterPassword;
  17. 17         action[C_REGISTER_EMAIL]    = &action_RegisterEmail;
  18. 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. $
复制代码
发表于 2007-11-14 17:19:15 | 显示全部楼层
不行就改成static,参数加上this
回复 支持 反对

使用道具 举报

 楼主| 发表于 2007-11-14 17:20:29 | 显示全部楼层
我试过了,一样不通过。
回复 支持 反对

使用道具 举报

发表于 2007-11-14 17:51:47 | 显示全部楼层
Say '&protocolController::action_NULL'
这样??
回复 支持 反对

使用道具 举报

 楼主| 发表于 2007-11-16 17:11:53 | 显示全部楼层
问题已经解决,楼上的正解。我竟然加了没用的括号。
有时候“画蛇添足”还真是不经意的。
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

快速回复 返回顶部 返回列表