lundi 29 juin 2015

Matrix c++ implementation

I tried to code matrix class in c++11. I am going to use it Berlekamp factorization; I got few errors while compilation. Please look at my code;

matrix.h

template <class T>
class Matrix {
private:
    T** m_table;
    size_t m_size;
public:
    Matrix(int size);
    Matrix(int size, const std::function<T(int, int)> & generator);
    Matrix(const Matrix & m);

    void set(int i, int j, T t);

    void resize(size_t size);

    T get(int i, int j) const;

    ~Matrix();
};

matrix.cpp

#include "matrix.h"


template <class T>
Matrix<T>::Matrix(int size): m_size(size) {
    m_table = new T*[size];
    for (size_t i = 0; i <size; i++) {
        m_table[i] = new T[size];
    }
}


template <class T>
Matrix<T>::Matrix(int size, const std::function<T(int i, int j)> & generator):
    Matrix(size) {
    for (size_t i = 0; i < size; i++) {
        for (size_t j = 0; j < size; j++)
            m_table[i][j] = generator(i, j);
    }
}



template <class T>
Matrix<T>::~Matrix() {
   if (m_size > 0)
       for (T* t : m_table)
           delete [] t;
}

template <class T>
T Matrix<T>::get(int i, int j) const {
    return m_table[i][j];
}

template <class T>
void Matrix<T>::set(int i, int j, T t) {
    m_table[i][j] = t;
}

template <class T>
Matrix<T>::Matrix(const Matrix & m) {
    resize(m.m_size);
    for (size_t i = 0; i < m_size; i++) {
        for (size_t j = 0; j < m_size; j++)
            m_table[i][j] = m.get(i, j);
    }
}


template <class T>
void Matrix<T>::resize(size_t size) {
    if (m_size > 0)
        for (T* t : m_table)
            delete [] t;
    m_size = size;
    m_table = new T*[size];
    for (size_t i = 0; i <size; i++) {
        m_table[i] = new T[size];
    }
}

In main.cpp

error: undefined reference to Matrix::~Matrix()

Matrix<GaloisFieldElement> Q = buildQ(p);

in buildQ.cpp

    ...
    auto generator = [&] (int i, int j) -> GaloisFieldElement {
        return get(p, i, j);
    };

error: undefined reference to Matrix::Matrix(int, std::function&)

    return Matrix<GaloisFieldElement>(deg, generator);
}

Where I made mistake?

Aucun commentaire:

Enregistrer un commentaire