繁体   English   中英

错误:没有 object 无法呼叫成员 function - 但我有一个 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())) {

这是相关代码。 我不明白为什么它不会编译,因为我确实提供了一个非常具体的成员 object。我觉得我应该补充说 consolePlay 是一个全局/独立的 function。

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 访问指向命令 class 的不同成员的指针数组中的成员,该数组本身是 CommandProcessing 的成员变量。

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

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

如果 function validate不是 static 成员 function 那么您需要指定一个 object,例如

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM