-3

everyone. I have problem about the static function.

In the Employee.h, I wrote

class Employee

{

public:

static int getCount();

private:


static int count;

}

In the Employee.cpp,

static int Employee::getCount()

{

    return count;

}

But when I compile, the error shows

"error C2724: 'Employee::getCount': 'static' should not be used on member functions defined at file scope"

Can somebody show me how to fix it? Thanks.

user0042
  • 7,917
  • 3
  • 24
  • 39
Meiyi Zheng
  • 9
  • 2
  • 4
  • 5
    Remove `static` in your cpp and define `count`. – skypjack Oct 14 '17 at 09:09
  • 1
    What about following the instruction in the error message and remove the `static` keyword from the function definition? – user0042 Oct 14 '17 at 09:10
  • Possible duplicate of [The static keyword and its various uses in C++](https://stackoverflow.com/questions/15235526/the-static-keyword-and-its-various-uses-in-c) – user7860670 Oct 14 '17 at 09:15

1 Answers1

1

The static in front of your member function means that "this function is static", not "returned type of this function is static", and there is nothing like "return a static type". You just return an int from your member function getCount(). Its value is copied from a static field but this is the implementation detail of this function, which has nothing to do with its signature. You are free to decide whether your member function should be static or not.

And for your code to compile, remove the static keyword from your function definition in your Employee.cpp.

int Employee::getCount()
{
    return count;
}
Jason Zang
  • 101
  • 3