0

I have a class Product:

class Product{
public:
    int weight;
    static *Product listOfProducts;
}

int main(){
    Product ProductList[100];
    *Product listPointer;
    listPointer = ProductList;
    
    Product::listOfProducts = listPointer;
}

I want to get a static pointer field to an array of this class' instances, but I have a bad understanding of how to do this.

2
  • 2
    The provided code is invalid and does not make a sense. Commented Dec 15, 2021 at 18:00
  • I guess you want an array of instances of the class (not an array of the class members) Commented Dec 15, 2021 at 18:18

1 Answer 1

2

Your syntax is wrong. Namely, you have placed * in the wrong places, you are missing a semicolon after the Product class declaration, and you are missing a definition for the Product::listOfProducts variable.

Try this instead:

class Product{
public:
    int weight;
    static Product* listOfProducts;
};

Product* Product::listOfProducts;

int main(){
    Product ProductList[100];
    Product* listPointer;
    listPointer = ProductList;
    
    Product::listOfProducts = listPointer;

    // or simply:
    // Product::listOfProducts = ProductList;
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks my Brother. My freaking program finally works

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.