The solution from the "duplicated from" question is to initialize the pointer after the class definition but I have found no success doing so. 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 find a way to define B::AArray anywhere in the code.
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