Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8671285
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T18:54:48+00:00 2026-06-12T18:54:48+00:00

So I am making a small game in C++ and I have run across

  • 0

So I am making a small game in C++ and I have run across a problem. I have a class called player inside my player.h file, and inside this class I have a public function called getPotion(). I also have a private static variable called potion. I have the exact same thing for the players health, and the getHealth() function returns the private static int playerHealth perfectly. But for apparently no reason, the getPotion function doesn’t return the potion. I get an error instead. I’ve also included the header file in all my other files.

Here’s the code:

(Sorry, I don’t know how to insert code, so I have to write it out)

player.h (the code that I’m having trouble with):

class Player{
private:
    static int potions;

public:
    int getPotions();
}

player.cpp (again the code I have trouble with):

int Player::potions;

int Player::getPotions(){
    Player player;
    return player.potions;
}

I have probably left out some bits of code like return and such, but that’s because I have a small amount of time to ask this question, so I put the parts that related to my problem.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T18:54:49+00:00Added an answer on June 12, 2026 at 6:54 pm

    First off, you are trying to return a static member of a class as if it were instantiated member of the object. Static members are referred to by Class::member, not object.member.

    Second, I don’t think you want potions to be static. Static members are shared between all objects of the class. So if player A has 100 health potions, then player B would have the same 100 health potions.

    Third, you declare Player::potions at the top of your .cpp file. I don’t think that’s what you want. The potions member was already declared in your .h file.

    player.h:

    class Player
    {
        private:
            int potions;
    
        public:
            int getPotions();
    };
    

    player.cpp:

    int Player::getPotions()
    {
        return potions;
    }
    

    If you DID want potions to be static, then change it to:

    return Player::potions;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am current making some small JS game in Netbeans and I have started
I have been making a small game for fun. In the game you are
I am making a small iOS game, and have finished everything, except the sound.
I am making a small game, with a friend. Now, I have a camera-system,
I'm making a small rpg game, and have implemented a sort of observer design
I have a school project where we are making a small JavaScript game without
I'm making a small game and I have a character that can move in
Hello im new here i am making a small tictactoe game where i have
I'm making a small game engine and need to have a memory pool associated
I am making a small hobby game, I have a couple of classes so

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.