error: cannot call member function without object - but I have an object?

error: cannot call member function without object - but I have an object?

我正在尝试编译以下代码,但收到此错误:

CommandProcessing.cpp:86:58: error: cannot call member function ‘bool CommandProcessor::validate(std::string)’ without object
   86 |     if (CommandProcessor::validate(game->currentCommand())) {

这是相关代码。我不明白为什么它不能编译,因为我确实提供了一个非常具体的成员对象。我觉得我应该补充说 consolePlay 是一个 global/independent 函数。

bool consolePlay(CommandProcessor* game) {
 83     cout << "game state: " << game->currentCommand();
 84     game->getCommand();
 85 
 86     if (CommandProcessor::validate(game->currentCommand())) {
 87         if (game->currentCommand() == "loadmap") {
 88             game->setState("maploaded");
 89             return true;
120 int main(int argc, char *argv[]) {
121     
122     if (argc == 0) {
123         cout << "You must specify console or file input in command line" << endl;
124         exit(0);
125     }
126 
127     if (argc > 1 && (argv[0] != "-console" || argv[0] != "-file")) {
128         cout << "Invalid command line argument(s)" << endl;
129         exit(0);
130     }
131     CommandProcessor *game = new CommandProcessor();
132     if (argv[argc-1] == "console")
133         while (consolePlay(game)) {
134             continue;   
135         }

currentCommand 访问指向 Command class 不同成员的指针数组中的成员,该数组本身是 CommandProcessing 的成员变量。

22 string CommandProcessor::currentCommand() {
 23     Command temp = *commandList[commandCount];
 24     return temp.command;
 25 }

我很感激这让我发疯的帮助。

如果函数validate不是静态成员函数那么你需要指定一个调用它的对象,例如

if (game->validate(game->currentCommand())) {