簡體   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