Im am making my first game and one of the files just won't compile.


I keep getting this error whenever I try to compile this file (this is only the problem part):

if (answer[0] == 'y')
     {
                   
           if (player->GetGold >= 10)   // PROBLEM
           {
               player->SpendGold(10);
               cout << "You find a nice bed and fall fast asleep.";
               Wait();
           
               cout << "\n(HP and MP restored\n\n";
               cout << "HP: " << player->GetHealth() << " / " << player->GetMaxHealth() << "\n";
               cout << "MP: " << player->GetMana() << " / " << player->GetMaxMana() << "\n";
               player->SetHealth(player->GetMaxHealth());
               player->SetMana(player->GetMaxMana());
               Wait();
           }
           else
           {
               cout << "It looks like you don't have enough gold.\n";
               Wait();
               answer[0] = 'n';
           }
     }

Dani AI

Generated

Short diagnosis and a minimal fix for 's compile error.

The compiler message means a member (function or variable) is being used where a value is expected. In this thread the usual culprit is that GetGold is a method but was not called — the code names the member instead of calling it. The simplest, correct condition looks like:

if (player->GetGold() >= 10) {
    player->SpendGold(10);
    ...
}

Checklist to resolve and avoid similar errors

  • Inspect the Player declaration: is GetGold declared as int GetGold() const; (a function) or as a data member (e.g., int gold; or std::string GetGold;)? If it’s a function you must use () to call it.
  • If GetGold really is a string (as suggested could be possible), convert it to an integer before comparing (or, better, change the API so getters return numeric types for numeric data).
  • If you intentionally wanted a pointer-to-member, that’s a different syntax — but that’s rarely what you want here.
  • Turn on full compiler warnings (e.g. -Wall -Wextra or /W4) and paste the exact compiler error and the Player header if the problem persists — as asked, the declaration is the key detail.

Small style note: make getters clearly named and typed (e.g. int GetGold() const;) so these mistakes are harder to make. This addresses the immediate compile error without changing the game logic.

Recommended Answers

All 3 Replies

That's not a whole file, so how is one to replicate your problem, or even undertand what we're looking at? Without the variables and function parameters, it's just some code. Which line does the error message point to?

That's not a whole file, so how is one to replicate your problem, or even undertand what we're looking at? Without the variables and function parameters, it's just some code. Which line does the error message point to?

the problem line is:

if (player->GetGold >= 10)

this is only 1 cpp file out of many

You can't compare the string player->GetGold with an integer. If that doesn't solve the problem, give us the information we need to understand the problem. Try reading the sticky posts at the top of the forum for some suggestions.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.