简体   繁体   中英

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

I'm trying to compile the following code but I receive this error:

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

Here is the relevant code. I don't understand why it won't compile because I do provide a very specific member object. I feel I should add that consolePlay is a global/independent 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 accesses a member in an array of pointers to different members of the Command class, that array itself being a member variable of CommandProcessing.

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

I appreciate the help this is driving me crazy.

If the function validate is not a static member function then you need to specify an object for which it is called as for example

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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