Name control exercise problem

Thinking in c++ has this question :
Create a class containing an int, a constructor that intializes the int from it's argument , and a print() function to display the int. Now create a second class that contains a static object of the first one. Add a static member function that calls the static object's print() function .exercise your class in main().
I have written the following code undefined refernce to withStatic :: WS. Someone please tell me what's wrong?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
  #include<iostream>

using namespace std;

class withoutStatic
{
      int x;
      public :
             withoutStatic(int s)
             {
                            x= s;
             }
             void printx()
             {
                  cout << "x " << x <<endl;
              }
};

class withStatic
{
      public :
             static withoutStatic WS;
             static void callwsprint()
             {
                    WS.printx();
             }
};

int main()
{
    withStatic WS1;
    WS1.callwsprint();
    system("pause");
    return 0;
}
You never initialized WS. You have to initalize static variables outside the class add this after the withStatic class: withoutStatic withStatic::WS(somevalue);

*edit: for more information please seek the middle of http://www.cplusplus.com/doc/tutorial/templates/
Last edited on
Thanks it's working now :)
Topic archived. No new replies allowed.