0

After reading StackOverflow's discussions and implementing some advices, I have these pieces of code intended just to test the behavior of static members of a class. Here is the header, which has the class declaration:

class OurClass
    {
    private:
        static int x, y;

    public:
        static void setVals(int valx, int valy);
        static int getValx();
        static int getValy();
        static void initialize();
    };

And here is the cpp file, which has the definition of these members as well as the main() function:

#include <iostream>
#include "OurClass.hpp"

using namespace std;

void OurClass::initialize()
    {
    static int x = 0;
    static int y = 0;
    }

void OurClass::setVals(int valx, int valy)
    {
    static int x = valx;
    static int y = valy;
    }

int OurClass::getValx()
    {
    static int x;
    return x;
    }

int OurClass::getValy()
    {
    static int y;
    return y;
    }


int main(void)
{
OurClass::inicializa();

cout << "Provide x and y..." << endl;

OurClass::setVals(cin.get(), cin.get());

cout << "Value of x: " << OurClass::getValx() << endl;
cout << "Value of y: " << OurClass::getValy() << endl;
return 0;
}

So, assuming that a static variable exists for the class, and that static functions only access static variables, I was expecting that x and y would have the values that we read from the keyboard with the setVals() call in main(). But when printing their values in the couts, they still have the value we assigned in the initialize() function (which BTW was another suggestion I got here, that is, initialize a static variable in a method). I am also unable to refer directly to the variables by OurClass::x or y even if I make them public. Do you guys know why?

5
  • 1
    Every time you write static int y; you define a function-scope local static variable. Those are different for each function and they are different from the classes static member variables. If you want to use those only, you have to remove the variable definitions and just use x and y. Commented Apr 5, 2018 at 9:37
  • All of your functions are redefining x and/or y as statics. So each function is modifying a variable that is local to it, rather than the ones declared at file scope. This typically results from copying declarations into functions (e.g. a copy/paste problem) so I'm voting to close as a typographic error. Commented Apr 5, 2018 at 9:45
  • Why did you mark everything under the sun as static? Seriously, why? Commented Apr 5, 2018 at 9:51
  • Peter and Jodocus, thanks for your time in assisting. However, what you suggest is what I have tried before asking the question. Referencing x or y from inside these methods, with or without the class name, makes g++ launch an error: undefined reference to `OurClass::x' (as I already explained to the other 2 guys). Commented Apr 5, 2018 at 10:02
  • @StoryTeller because I am testing the fundamentals of the language regarding static members. I know I can do something else work without resorting to it, but if you don't know the answer, that's perfectly fine. Commented Apr 5, 2018 at 10:06

2 Answers 2

1

First you need to access your local class's static variable instead declaring new method local variables in each method. Check below.

class Out {
private:
    static int x, y;

public:
    void set(int x, int y);
    int getSum();
};

int Out::x = 0;
int Out::y = 0;

void Out::set(int x, int y) {
    Out::x = x;
    Out::y = y;
}

int Out::getSum() {
    return Out::x + Out::y;
}
Sign up to request clarification or add additional context in comments.

1 Comment

When I try to access any of the members by doing OurClass::x or OurClass::y, g++ will fail with these messages: undefined reference to `OurClass::x'
1

Instead of setting values of existing variables this code creates new local static variables. Fix:

void OurClass::initialize()
{
    x = 0;
    y = 0;
}

void OurClass::setVals(int valx, int valy)
{
    x = valx;
    y = valy;
}

int OurClass::getValx()
{
    return x;
}

int OurClass::getValy()
{
    return y;
}

And add the definitions of those static variables in a .cc file (not header):

int OurClass::x;
int OurClass::y;

3 Comments

This is what I first tried, but if I reference the class variable with or without the class name, g++ will launch this error: undefined reference to `OurClass::x'
@JayY Updated the answer for you.
Maxim, your suggestion of defining the variables in the cc, prior to their use in the static functions, solved it. So it is indeed necessary to have them defined too. This is the solution. THANKS A LOT.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.