Reputation: 480
I am getting an error when trying to compile my class.
The error:
Matrix.cpp:13: error: expected constructor, destructor, or type conversion before ‘::’ token
Matrix.h
#ifndef _MATRIX_H
#define _MATRIX_H
template <typename T>
class Matrix {
public:
Matrix();
~Matrix();
void set_dim(int, int); // Set dimensions of matrix and initializes array
unsigned int get_rows(); // Get number of rows
unsigned int get_cols(); // Get number of columns
void set_row(T*, int); // Set a specific row with array of type T
void set_elem(T*, int, int);// Set a specific index in the matrix with value T
bool is_square(); // Test to see if matrix is square
Matrix::Matrix add(Matrix); // Add one matrix to another and return new matrix
Matrix::Matrix mult(Matrix);// Multiply two matrices and return new matrix
Matrix::Matrix scalar(int); // Multiply entire matrix by number and return new matrix
private:
unsigned int _rows; // Number of rows
unsigned int _cols; // Number of columns
T** _matrix; // Actual matrix data
};
#endif /* _MATRIX_H */
Matrix.cpp
#include "Matrix.h"
template <typename T>
Matrix<T>::Matrix() {
}
Matrix::~Matrix() { // Line 13
}
main.cpp
#include <stdlib.h>
#include <cstring>
#include "Matrix.h"
int main(int argc, char** argv) {
Matrix<int> m = new Matrix<int>();
return (EXIT_SUCCESS);
}
Upvotes: 0
Views: 2123
Reputation: 2516
Define destructor as
template <typename T>
Matrix<T>::~Matrix() {
}
And other error is that you cannot place implementation of class template in .cpp-file Template Factory Pattern in C++
Upvotes: 3
Reputation: 355069
Matrix::~Matrix() { }
Matrix
is a class template. You got the constructor definition right; the destructor (and any other member function definitions) need to match.
template <typename T>
Matrix<T>::~Matrix() { }
Matrix<int> m = new Matrix<int>();
This won't work. new Matrix<int>()
yields a Matrix<int>*
, with which you cannot initialize a Matrix<int>
. You don't need any initializer here, the following will declare a local variable and call the default constructor:
Matrix<int> m;
Upvotes: 6