0

I'm very new to programming and just starting out with C++ through a OOP course on the net. so, I'm trying to write a matrix class using templates and having a hard time with the constructor. I'm getting two errors:
1."no default constructor exists for class"
2."no default constructor exists for class"

#include <iostream>
#define DEFAULT 0

    template <int rows, int cols, typename T = int >

class Matrix
{
    T** matrix;
    int rows;
    int cols;

public:
     Matrix(int rows, int cols, T num = (int num = DEFAULT))
    {
        this.rows = rows;
        this.cols = cols;
        this.matrix = new Matrix[rows][cols];

        for (int i = 0 ; i<rows ; i++)
        {
            for (int j = 0; j < cols; j++)
            {
                matrix[i][j] = num;
            }
        }

    }

and the main:

#include <iostream>
#include "matrix.h"

int main()
{
  Matrix<4, 4> mat;
}

I have seen quiet a few examples but there is still something I'm missing, perhaps serval things. thanks for the help, Starter.

  • Why didn't you pass any parameters when constructing `mat`? – Mooing Duck May 05 '21 at 15:57
  • When creating questions, prefer to post the full and complete error message. If you're using Visual Studio, this is NOT in the "Error" pane, and is instead in the "Output" pane. – Mooing Duck May 05 '21 at 15:58
  • "...starting out with C++ through a OOP course on the net" ouch. Be aware of sites that call themselve "tutorial". I have seen many poor, some that are actually doing harm to beginners, but none really good. It is usually recommended to learn from [books](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). – 463035818_is_not_an_ai May 05 '21 at 16:04
  • some will hate me for this one: A templated matrix class isnt for day1. This is not how you allocate memory for a matrix `this.matrix = new Matrix[rows][cols];`. I suggest you to start a little lower. Try to write a class that mangages a `T*` pointing to a single `T` for example, you will be surprised how many mistakes one can make with that. – 463035818_is_not_an_ai May 05 '21 at 16:09
  • first of all thanks to both of you for responding! as far as to why I didn't pass any parameters. I should have mentioned I didn't wrote the main. I got an output (4x4 matrix with zeros) and that line of code, so what I needed to do is to write the class so it will support that (with template). and regarding the allocation. I guess I do need to go over this more and exercise. – Starter May 05 '21 at 16:21

0 Answers0