trying to build a basic linear algebra library

closed account (E093605o)
I am trying to learn C++ and want to build a Matrix library. So far I have a Matrix.h file:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
//Matrix.h
#pragma once
#include <vector>

template<typename Type>
class Matrix {

  size_t cols;
  size_t rows;

 public:
  std::vector<Type> data;
  std::tuple<size_t, size_t> shape;
  size_t elementCount;
  void print();

  /* constructors */
  Matrix(size_t rows, size_t cols);
  Matrix();

  // methods
  void print();
  // ...

};

//Matrix.cpp

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

void Matrix::print(){
  for (int i = 0; i < cols; i++){

  }
}


Now, I want to implement the methods in a Matrix.cpp file.
But my IDE (vscode) throws two errors: argument list for class template "Matrix" is missing
and: identifier "cols" is undefined
Can somebody tell me why this happens? Thanks!
You have two void print function declarations in your header. Ooops!

With templates you can't split the declarations and definitions into header and source files.

Plus, the function declaration and definition needs to match exactly, when using templates it gets a bit 'wordy.'

Matrix.h:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#pragma once

#include <vector>
#include <iostream>

template<typename Type>
class Matrix
{
private:
   size_t cols { 0 };
   size_t rows { 0 };

public:
   std::vector<Type> data;
   std::tuple<size_t, size_t> shape;
   size_t elementCount;

   void print();

   /* constructors */
   Matrix(size_t num_rows, size_t num_cols) : cols(num_cols), rows(num_rows) { };
   Matrix() { };
};

template <typename Type>
void Matrix<Type>::print()
{
   for ( int i { }; i < cols; i++ )
   {
      std::cout << i << ' ';
   }
   std::cout << '\n';
}

source.cpp:
1
2
3
4
5
6
7
8
9
10
#include <iostream>

#include "Matrix.h"

int main()
{
   Matrix<int> m(3, 4);

   m.print();
}
0 1 2 3
closed account (E093605o)
thanks for your help. So now I have this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
//Matrix.h
#pragma once
#include <vector>

template<typename Type>
class Matrix {

  size_t cols;
  size_t rows;

 public:
  std::vector<Type> data;
  std::tuple<size_t, size_t> shape;
  size_t elementCount;

  /* constructors */

  Matrix(size_t rowsArg, size_t colsArg) : cols(colsArg), rows(rowsArg)({
    data = std::vector<std::vector<Type>(cols)>(rows);
  };

  Matrix();

  //methods
  void print();
  

};

// methods
template<typename Type>
void Matrix<Type>::print(){
  for (int i = 0; i < rows; i++){
    for (int j = 0; j < cols; j++){
      cout << data[i][j] << " ";
    }
    cout << std::endl;
  }
}

//Matrix.cpp
#include "Matrix.h"
#include <iostream>



int main(){
  Matrix<float> test((size_t) 2, (size_t) 3);
  return 0;
}


But I get two errors, in Matrix.h:"class template "Matrix<Type>" has no member "print" "although I made sure that I saved all the changes and as you can see - print is in there. Also I get "no instance of constructor "Matrix<Type>::Matrix [with Type=float]" matches the argument list" in Matrix.cpp
Line 18: Remove ( before {

Line 20 19: You cannot pass cols together with the template arguments like that.

After fixing these issues and adding missing includes and std:: the code compiles.
Last edited on
closed account (E093605o)
I don't really know how to fix line 20 (I think you mean 19), I have searched a bit on SO and another forum but their questions didn't relate precisely to mine.
As 1 file, then possibly something like:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#include <vector>
#include <iostream>

template<typename Type>
class Matrix {
	size_t cols {};
	size_t rows {};

public:
	std::vector<std::vector<Type>> data;
	//std::tuple<size_t, size_t> shape;
	size_t elementCount {};

	Matrix(size_t rowsArg, size_t colsArg) : cols(colsArg), rows(rowsArg) {
		data.resize(rows);

		for (auto& c : data)
			c.resize(cols);
	}

	Matrix() {}

	void print() const;
};

template<typename Type>
void Matrix<Type>::print() const {
	for (const auto& r : data) {
		for (const auto& e : r)
			std::cout << e << ' ';

		std::cout << '\n';
	}
}

int main() {
	Matrix<float> test(2, 3);

	test.print();
}

closed account (E093605o)
thanks for your efforts. It still does not compile for some reason, although I can see no meaningful syntactic difference:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#pragma once
#include <vector>

template<typename Type>
class Matrix {

  size_t cols{};
  size_t rows{};

 public:
  std::vector<std::vector<Type> data;
  //std::tuple<size_t, size_t> shape;
  size_t elementCount{};

  /* constructors */
  Matrix(size_t rowsArg, size_t colsArg) : cols(colsArg), rows(rowsArg),
  {
		data.resize(rows);

		for (auto& c : data)
			c.resize(cols);
  }

  Matrix(){};

  //methods
  void print();
  

};

// methods
template<typename Type>
void Matrix<Type>::print(){
  for (int i = 0; i < rows; i++){
    for (int j = 0; j < cols; j++){
      cout << data[i][j] << " ";
    }
    cout << std::endl;
  }
}

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



int main(){
  Matrix<float> test(2, 3);
  /* no instance of constructor "Matrix<Type>::Matrix [with Type=float]" matches 
  the argument list
  */
  return 0;
}
What errors are you getting?

PS You need to have #include <iostream> before the class definition as print() uses it and cout needs to be std::cout

Last edited on
closed account (E093605o)
the error is in the comment in the main function: no instance of constructor "Matrix<Type>::Matrix [with Type=float]" matches the argument list
no instance of constructor "Matrix<Type>::Matrix [with Type=float]" matches the argument list

That error complains about your constructor.

Line 11: You need to add another >

Line 16: remove comma at the end of the line

After these fixes (and adding missing includes and std::) your code compiles again.


I don't really know how to fix line 20 (I think you mean 19)

Yes, line 19, sorry.

After the change that seeplus made to the type of data I think what you tried to do was something like:
1
2
3
4
5
6
Matrix(size_t rowsArg, size_t colsArg)
:	cols(colsArg),
	rows(rowsArg)
{
	data = std::vector<std::vector<Type>>(rows, std::vector<Type>(cols));
}

or using the constructor initialization list:
1
2
3
4
5
6
Matrix(size_t rowsArg, size_t colsArg)
:	cols(colsArg),
	rows(rowsArg), 
	data(std::vector<std::vector<Type>>(rows, std::vector<Type>(cols)))
{
}
Last edited on
Yeas - much better. I tried that originally but obviously got an error due to the incorrect definition for data. I was messing around with the constructor before I realised the definition type was incorrect. I forget to change the constructor code back as I was doing something else at the time. Ahhh..

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#include <vector>
#include <iostream>

template<typename Type>
class Matrix {
	size_t cols {};
	size_t rows {};

public:
	std::vector<std::vector<Type>> data;
	//std::tuple<size_t, size_t> shape;
	size_t elementCount {};

	Matrix(size_t rowsArg, size_t colsArg) : cols(colsArg), rows(rowsArg),
		data(std::vector<std::vector<Type>>(rows, std::vector<Type>(cols))) {
	}

	Matrix() {}

	void print() const;
};

template<typename Type>
void Matrix<Type>::print() const {
	for (const auto& r : data) {
		for (const auto& e : r)
			std::cout << e << ' ';

		std::cout << '\n';
	}
}

int main() {
	Matrix<float> test(2, 3);

	test.print();
}

closed account (E093605o)
thanks all of you, my "IDE" did not highlight all these syntax errors. it works now!
If you get a long list of errrors it's usually best to start with the one at the top because later errors can be caused by earlier ones.
Last edited on
also try to find the first error that relates to your code. You might first get errors relating to the c++ standard library. That will be the line in your code that is causing an error.
Topic archived. No new replies allowed.