c++ - 函数指针指向静态成员类的错误 C2065

标签 c++ function pointers static

我正在尝试更改 header 类中用作创建数据库的基础的静态常量结构的现有代码。目前的代码是

//database.h

#define VIDEODB_TYPE_INT 1
const struct DBHeaders
{
  std::string name;
  std::string dbType;
  int type;
} DBHeadersTable1[] = 
{
  { "idRow", "INTEGER PRIMARY KEY", VIDEODB_TYPE_INT},
  { "value", "INTEGER", VIDEODB_TYPE_INT}
};

const struct DBHeaders DBHeadersTable2[] = 
{
  { "idRow", "INTEGER PRIMARY KEY", VIDEODB_TYPE_INT},
  { "value", "INTEGER", VIDEODB_TYPE_INT}
};

class CDatabase
{
public:
  void getDatabaseInteger(DatabaseRow& details);
  void get(int column, DBHeaders headers, DatabaseRow& details)
  {
    if (headers[i].type == VIDEODB_TYPE_INT)
      getDatabaseInteger(details);
  }
  //other functions
}

但这种方法不再适用,因为现在我们有需要更改的字段才能使用。因此,我不想给出一个代表函数的数字,而是直接插入一个指向函数的指针,从而提供更大的灵 active 。这是我的新代码

//database.h

typedef void (*getFunctionType)(DatabaseRow&);
const struct DBHeaders
{
  std::string name;
  std::string dbType;
  getFunctionType getFunction;
} DBHeadersTable1[] = 
{
  { "idRow", "INTEGER PRIMARY KEY", &(CDatabase::getDatabaseInteger)},
  { "value", "INTEGER", &(CDatabase::getDatabaseInteger)}
};

const struct DBHeaders DBHeadersTable2[] = 
{
  { "idRow", "INTEGER PRIMARY KEY", &(CDatabase::getDatabaseInteger)},
  { "value", "INTEGER", &(CDatabase::getDatabaseInteger)}
};

class CDatabase
{
public:
  static void getDatabaseInteger(DatabaseRow& details);

  //other functions
}

我的想法是为我的行定义一个常量和一个指向代码必须用来解析列的函数的指针。我得到的错误是: https://msdn.microsoft.com/en-us/library/ewcf0002.aspx 在线上

  { "idRow", "INTEGER PRIMARY KEY", &(CDatabase::getDatabaseInteger)},

在错误的括号之间我没有“database.h”而是另一个文件... 是否可以通过这种方式指向一个静态函数?我做错了什么吗?

最佳答案

根据您发布的代码,在我看来您需要声明该类:

class CDatabase
{
public:
  static void getDatabaseInteger(DatabaseRow& details);

  //other functions
};

同一头文件中引用CDatabase::getDatabaseInteger() 方法的所有数组之前。看起来在你的头文件中你在实际声明它之前引用了静态函数。将此声明移至文件开头。

关于c++ - 函数指针指向静态成员类的错误 C2065,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34742914/

相关文章:

c++ - 两个函数变量到一个函数

c++ - 计算特定数字的递归函数

c++ - int(*a)的解释[3]

c++ - 字符串中的十六进制到整数中的十六进制

c++ - Linux 上第一对读写后串口挂起

c++ - 在 C 和 C++ 中的函数指针和对象指针之间转换

linux - Bash - 创建 'goto' 等价物的问题

javascript - Typeof 运算符问题

pointers - 在汇编(emu8086)中进行内存寻址时,bp和si有什么不同?

c - 为什么将指针作为输入参数传递会允许 "swapping"和其他可能的意外副作用?