5

I searched the Internet and found that some people said that non-static member function can access static member function or data. Then I wrote a program to verify it.

#include <iostream>
class test
{
public:
    static int a;
    void printa()
    {
        std::cout<<a;
    }
};

int main(int argc, const char * argv[])
{
    test m;
    m.printa();

    return 0;
}

The code generate linker errors!

Undefined symbols for architecture x86_64:
  "test::a", referenced from:
      test::printa() in main.o
user2261693
  • 405
  • 3
  • 7
  • 11
  • possible duplicate of [Linker error when using static members](http://stackoverflow.com/questions/8612206/linker-error-when-using-static-members) – AnT stands with Russia May 09 '13 at 06:55
  • Here is similar problem and its solution is [Here][1] [1]: http://stackoverflow.com/questions/8034568/undefined-symbols-for-architecture-x86-64-which-architecture-should-i-use – Uahmed May 09 '13 at 06:55
  • possible duplicate of [Initializing private static members](http://stackoverflow.com/questions/185844/initializing-private-static-members) – Joris Timmermans May 09 '13 at 07:21

4 Answers4

8

Declaring a variable as static inside a class is, well, only a declaration.

You need to define the variable as well, which means adding this line in a single compilation unit :

int test::a = 0;

To be more precise : a compilation unit is basically a .cpp file. You should not put that line directly in a header file, otherwise you will get the opposite error : "multiple definition of...".

This will also, as you have guessed, initialize your variable to 0 once your program starts.

If you put this line under your class declaration, it will fix your problem (in this specific situation : remember not to write this in a header file).

Nbr44
  • 2,042
  • 13
  • 15
  • 2
    worth adding "don't put it in a header file", since not everybody knows what "compilation unit" means. – Elazar May 09 '13 at 07:09
2

That's because you've only declared test::a, not defined it:

#include <iostream>
class test
{
    ...
};

int test::a = 1; //Needs a definition!
Yuushi
  • 25,132
  • 7
  • 63
  • 81
1

You have only declared the static data member. You have not defined it.

YOu need to do something like int test:: a; to define it.

See this too

Non-static members are allowed to access static data members. The reverse is not allowed because static members do not belong to any object

Community
  • 1
  • 1
Suvarna Pattayil
  • 5,136
  • 5
  • 32
  • 59
0

You must define the static data member instance. Add a line...

int test::a;

...above main(), or under main()... basically anywhere directly in the same namespace scope as class test and after class test's definition.

Tony Delroy
  • 102,968
  • 15
  • 177
  • 252
  • it doesn't matter where it will be defined. – Elazar May 09 '13 at 06:56
  • @Elazar: that's a cyclic argument, because if you try to put it somewhere illegal it won't be defined ;-P. My suggestion of above `main` was to ensure that a definition wasn't *attempted* within `main()` (which would constitute another declaration), nor before or inside `class test`.... – Tony Delroy May 09 '13 at 07:01
  • @elazar that's not completely true. The declaration of the class that it is member of needs to be visible. – jrok May 09 '13 at 07:01
  • @TonyD then edit your answer to reflect that, otherwise it is misleading. – Elazar May 09 '13 at 07:05
  • @Elazar: I think "misleading" is a bit strong... but no harm so have clarified. Cheers. Maybe you should suggest Nbr44 make similar changes for "If you put this line under your class declaration, it will fix your problem."...? – Tony Delroy May 09 '13 at 07:08
  • @tonyD did i say declaration? Yes yes, definition :) – jrok May 09 '13 at 07:14