0

I want to build a simple matrix class. The relevant part of my header file looks like this:

template <typename T>
class matrix
{
private:
  unsigned int nrows;
  unsigned int ncols;
  std::array<std::array<T, ncols>, nrows> mat;

public:
  matrix();

  unsigned int getCols() const;
  unsigned int getRows() const;

};

The problem here is that the two-dimensional array (called mat) needs the number of rows and columns. Obviously, this doesn't work but I don't know how to solve this issue.

My source file:

template <typename T>
matrix<T>::matrix() : nrows(0), ncols(0) {}

template <typename T>
unsigned int matrix<T>::getCols() const {
  return ncols;
}

template <typename T>
unsigned int matrix<T>::getRows() const {
  return nrows;
}

The initialization of a matrix should look something like this:

matrix<double> my_matrix;
1
  • 2
    Use std::vector<std::vector> std::array<std::array> dimensions must be known at compile time. Commented May 9, 2015 at 9:14

1 Answer 1

1

You cant have variable size argument for size of array. Therefore you must have two more template arguments for matrix class.

template <typename T, size_t ROWS, size_t COLS>
class matrix
...
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you for the hint, I've implemented the class with std::vector now due to the need to push_back etc.

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.