C ++转换错误

时间:2014-02-11 10:28:30

标签: c++ class pointers member

我正在尝试实现以下指向成员函数数组的指针:

IOperand*       OpCreate::createOperand(eOperandType type,
                                        const std::string& val)
{
  size_t        it = 0;
  OpPtrTab      tab[] =
    {
      {Int8, &OpCreate::createInt8},
      {Int16, &OpCreate::createInt16},
      {Int32, &OpCreate::createInt32},
      {Float, &OpCreate::createFloat},
      {Double, &OpCreate::createDouble},
      {Unknown, NULL}
    };

  while ((tab[it]).type != Unknown)
    {
      if ((tab[it]).type == type)
        return ((tab[it]).*((tab[it]).funcPtr))(val);
      it++;
    }
  return NULL;
}

来自以下课程:

class   OpCreate
{
public :

  struct OpPtrTab
  {
    const eOperandType  type;
    IOperand*           (OpCreate::*funcPtr)(const std::string&);
  };                                                                               

  IOperand*     createOperand(eOperandType, const std::string&);

  OpCreate();
  ~OpCreate();

private :

  IOperand*     createInt8(const std::string&);
  IOperand*     createInt16(const std::string&);
  IOperand*     createInt32(const std::string&);
  IOperand*     createFloat(const std::string&);
  IOperand*     createDouble(const std::string&);
};

我看不出我做错了什么,但这是编译错误:

OpCreate.cpp: In member function ‘IOperand* OpCreate::createOperand(eOperandType, const string&)’:
OpCreate.cpp:66:39: error: pointer to member type ‘IOperand* (OpCreate::)(const string&) {aka IOperand* (OpCreate::)(const std::basic_string<char>&)}’ incompatible with object type ‘OpCreate::OpPtrTab’

似乎我的调用或初始化与原型不匹配,但我看不出原因。

1 个答案:

答案 0 :(得分:1)

您将成员函数指针tab[it].funcPtr应用于tab[it]对象,但它不指向tab[it]个对象的成员函数。

我认为您想将其应用于this

(this->*(tab[it].funcPtr))(val);