0

This completely sums up the problem I am having right now:

class A{
public:
    int Avar;
    A(int Avar = 0){this->Avar = Avar;}
};

class B{
private:
    static A *AArray;
public:
    static void setAArray();
};

int main(){
    return 0;
}

void B::setAArray(){
    A *data;
    data = new A[10];
    AArray = data; // <- delete this line and the program complies
}

This piece of code says I have an undefined reference to 'B::AArray' and won't compile. How do I resolve this if I want a static pointer to an array of class A objects in class B?

Note: my class does not use multiple files currently and everything is under one .cc file

Edit: the question that is marked as duplicate as does not deal with a pointer or with an array of objects from another class. Tried solution from said post with no results.

halfer
  • 19,824
  • 17
  • 99
  • 186
Potato Salad
  • 37
  • 1
  • 5
  • 1
    The duplicate's accepted answer tells exactly what you need to do: provide a definition for `A::AArray`. – Quentin Jan 23 '17 at 11:05
  • Trying to insert A B::AArray; or A B::AArray = new A[10]; gives me multiple errors including "conflicting declaration" and "cannot be initialized by a non-constant expression". I can't seem to figure out a way to define B::AArray anywhere – Potato Salad Jan 23 '17 at 11:46
  • 1
    You can define it anywhere at global scope under the definition of `B`, since you only have one cpp file. The syntax is simply `A *B::AArray;`. – Quentin Jan 23 '17 at 11:56

0 Answers0