0

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?

albbeltran
  • 31
  • 3
  • Why not simply `std::vector> matrix`? That takes care of the manual memory management. Not only that, a `for` loop would no longer be necessary to initialize the data. – PaulMcKenzie May 26 '22 at 18:34
  • 1
    Side note: Arrays of pointers to arrays can really slow down a program. Often you're better off making one big array of `m*n` and perform the 2D to 1D translation in the accessor functions. – user4581301 May 26 '22 at 18:41
  • 1
    Are you aware that `matricesToSum` is an array of *pointers* to `Matrix`? Was that your intent? – Bob__ May 26 '22 at 18:53
  • Yes Bob, thanks for noticing. The problem is that if I remove the pointer it gives me the error: [Error] no matching function for call to 'Matrix::Matrix()' – albbeltran May 26 '22 at 18:59
  • I think I will make a new post to learn how to create an array of template class object, because I get the error above. Thanks for your help! – albbeltran May 26 '22 at 19:02
  • @albbeltran `Matrix *matricesToSum[numMxToSum];` -- This is not valid C++. [Arrays in C++ must have their size denoted by a compile-time value](https://stackoverflow.com/questions/1887097/why-arent-variable-length-arrays-part-of-the-c-standard), not a runtime value such as `numMxToSum`. The proper way to do this is `std::vector> matricesToSum(numMxToSum);`. – PaulMcKenzie May 26 '22 at 19:11
  • @albbeltran [Example](https://godbolt.org/z/PdG77E66n). That entire code does what you're trying to do, both in declaring the number of matrices, and initializing all the matrices to a certain size. This is even before you could add the optimization of using a single vector. – PaulMcKenzie May 26 '22 at 19:20
  • Thanks @PaulMcKenzie. I'll use that syntax, I've never used std::vector before so I didn't know about it. I appreciate your help. – albbeltran May 26 '22 at 19:43
  • Note that you don't even need the `m` and `n` member variables if you use `std::vector`. In fact, I recommend you remove them. A `std::vector` knows is own size by calling the `size()` function, so `m` and `n` are unnecessary. Unnecessary variables meant to track the size of a vector is just a way to open yourself up to more bugs. What if those variables miss when the vector is resized? You now have a bug with those unnecessary size variables being out-of-sync with the actual size of the vector. – PaulMcKenzie May 26 '22 at 19:46
  • Why store the matrixes at all? Create a Matrix for the result and every time the user inputs a matrix add it to the result. No need to store the input past that point. – Goswin von Brederlow May 26 '22 at 19:54

1 Answers1

1

Seems to be a syntax problem you should replace:

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

To:

matricesToSum[i] = Matrix<float>(m,n);
Hugo
  • 11
  • 1