beginh
beginh

Reputation: 1153

Use of static object of one class in the other

I am trying to test calls for static object from one class in the other. I get here linking error LNK2001: unresolved external symbol "public: static class K G::ob1" I don't know what's wrong and on the internet I cannot find any info about static objects, only static with all other configurations. Therefore I ask for your help. Do I need to create object of K for this whole to work, or am I able to abstract so much that I don't create any objects?

#include <iostream>
using namespace std;

class K
{
    int a;
public:
    K(int x) { a = x; };
    void print() {  cout << " a is: " << a << endl; };
};

class G
{
public:
    static K ob1;
    static void printG()
    {
        ob1.print();
    };
};

int main()
{
    K o1(10);
    G::printG();
    system("pause");
}

Upvotes: 0

Views: 63

Answers (1)

clcto
clcto

Reputation: 9648

You have to define the static variable. It is similar to a global variable in C.

K G::ob1;
int main(){ .... }

Upvotes: 3

Related Questions