buddy
buddy

Reputation: 861

Access global pointer in another class

I have defined globally a list in one class as a pointer:

class1.cpp

type list[1000];
type *p_list = list;

Now I want to use this list and put some values into it. This should happen in another class, in a method

class2.cpp

mousePressEvent_from_class_2()
{
    p_list[counter].x = pos().x(); 
}

But the compiler is telling me that it doesn't know p_list. How can I change that?

Upvotes: 2

Views: 2060

Answers (1)

Praetorian
Praetorian

Reputation: 109119

The compiler needs to know that p_list is declared elsewhere. Put the following in class1.h or in class2.cpp (at file scope).

extern type *p_list;

The definition of type must also be visible in class2.cpp. Make sure the definition is in a header file (class1.h) and class2.cpp #includes this header.

Upvotes: 6

Related Questions