0

I have a class in which I declare a friendship to another class outside the namespace. Now that class has a static integer I want to access.

Here is the code:

    class A
    {
    public:
        void init();
    };


    namespace C
    {

    class B 
    {
       friend class ::A;

    private:
        static int i;
    };
    }


    void A::init()
    {
        C::B::i=3;
    }

    int main()
    {
        A f;

        f.init();

        return 0;
    }

Now if I compiled this code I get the following error:

  /tmp/ccmDINqo.o: In function `A::init()':
  friendclass.cpp:(.text+0xa): Undefined reference to `C::B::i'
  collect2: error: ld returned 1 exit status

Now for what I understand, the declaration is there so init() is actually trying to assign a value to i.

I think compilation works fine, but the linker seems to complain.

Why does this code not build and how can I fix it so it will build?

Gerhard Stein
  • 1,543
  • 13
  • 25

0 Answers0