简体   繁体   中英

Store dynamic matrix objects in an array C++ with template class

I have a template class of a dynamic matrix. I want to ask the user for the number of matrices they want to add, and then create the matrix objects within a for loop through storing them in an array.

This is the class constructor:

template <class E> 
class Matrix{
public:
    //CONSTRUCTOR
    Matrix(int m, int n) {
        this->m = m;
        this->n = n;
        matrix = new E*[m];
        for(int i=0; i<m; i++) {
            matrix[i] = new E[n];
        }
    }

For that, the array must be of the template class type:

        std::cout<<"\nWho many matrices do you want to sum?"; std::cin>>numMxToSum;
        Matrix<float> *matricesToSum[numMxToSum];           
        
        for(int i=0; i<numMxToSum; i++) {
            matricesToSum[i] = Matrix<float> matrixObjToSum(m,n);
        }

But when I try to run it, I get: "[Error] expected primary-expression before 'matrixObjToSum'"

How can I store multiple matrix objects inside an array?

Seems to be a syntax problem you should replace:

matricesToSum[i] = Matrix<float> matrixObjToSum(m,n);

To:

matricesToSum[i] = Matrix<float>(m,n);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM