tomstewart89 / BasicLinearAlgebra

A library for using matrices and linear algebra on Arduino
MIT License
187 stars 38 forks source link

Matrix without a specific row/column size #19

Closed davidbsp closed 6 years ago

davidbsp commented 6 years ago

Hi @tomstewart89,

Is it not possible to pass a matrix as parameter of a function without specifying the number of rows and columns?

I would like to have a function something like this: void function (BLA::Matrix P), and inside the function I would use GetColCount() and GetRowCount() to handle matrixes of any size.

Thanks!

tomstewart89 commented 6 years ago

Hi @davidbsp ,

Because Matrix is a template class, Matrix<2,1> and Matrix<2,2> (for example) are treated as distinct datatypes in C++. void function (BLA::Matrix P) expects just a single type so it's not possible to pass matrices of arbitrary size a non-template function. On the other hand, you can define a template function like so: template <int rows, int cols> void function(BLA::Matrix<rows,cols> P) which will more or less do what you're looking for. If you haven't come across them already, you can read up on template functions here: http://www.cplusplus.com/doc/oldtutorial/templates/

davidbsp commented 6 years ago

Hi again @tomstewart89! I solved it using a template function just like you suggested. Many Thanks!

tomstewart89 commented 6 years ago

No problem!