-3

I have problem with my VS 2017. Everytime i change the body of my method (e.g. multiplication of matirces) in the class.cpp file i have to rewrite instruction in main function. I.E. if I have an a = b * c; statement i must delete and add again multiplication symbol int the main in "test.cpp" and then compile. Otherwise VS will not include the change in my method and will act as there was no change at all in the method implementation.

Thanks in advance.

EDIT This is the code im trying to fix:

template<typename T>
Mx::Matrix<T>::Matrix(unsigned rows, unsigned cols, T const & init_val)
{
    _matrix.resize(rows);
    for (unsigned i = 0; i < _matrix.size(); i++)
        _matrix[i].resize(cols, init_val);
    _rows = rows;
    _cols = cols;
}

template<typename T>
Mx::Matrix<T>::Matrix(Matrix<T> const & mx)
{
    _matrix = mx._matrix;
    _rows = mx.GetRows();
    _cols = mx.GetCols();
}

template<typename T>
Mx::Matrix<T> & Mx::Matrix<T>::operator=(Matrix<T> const & mx)
{
    if (this == &mx)
        return *this;

    unsigned new_rows = mx.GetRows();
    unsigned new_cols = mx.GetCols();

    _matrix.resize(new_rows);
    for (unsigned i = 0; i < _matrix.size(); i++)
        _matrix[i].resize(new_cols);

    for (unsigned i = 0; i < new_rows; i++) {
        for (unsigned j = 0; j < new_cols; j++) {
            _matrix[i][j] = mx(i, j); // musisz przeciazyc operator()()
        }
    }

    _cols = new_cols;
    _rows = new_rows;
    return *this;
}

template<typename T>
Mx::Matrix<T> Mx::Matrix<T>::operator+(Matrix<T> const & mx) const
{
    Mx::Matrix<T> temp(mx); // ALBO Mx::Matrix<T> temp(_rows, _cols, 0.0)
    for (unsigned i = 0; i < this->GetRows(); i++) {
        for (unsigned j = 0; j < this->GetCols(); j++) {
            temp(i, j) = (*this)(i, j) + mx(i, j); // ALBO this->_matrix[i][j]
        }
    }
    return temp;
}

template<typename T>
Mx::Matrix<T>& Mx::Matrix<T>::operator+=(Matrix<T> const & mx)
{
    return *this = *this + mx;
}

template<typename T>
Mx::Matrix<T> Mx::Matrix<T>::operator-(Matrix<T> const & mx) const
{
    Mx::Matrix<T> temp(mx); // ALBO Mx::Matrix<T> temp(_rows, _cols, 0.0)
    for (unsigned i = 0; i < this->GetRows(); i++) {
        for (unsigned j = 0; j < this->GetRows(); j++) {
            temp(i, j) = (*this)(i, j) - mx(i, j); // ALBO this->_matrix[i][j]
        }
    }
    return temp;
}

template<typename T>
Mx::Matrix<T>& Mx::Matrix<T>::operator-=(Matrix<T> const & mx)
{
    return *this = *this - mx;
}

template<typename T>
Mx::Matrix<T> Mx::Matrix<T>::operator*(Matrix<T> const & mx)
{
    unsigned rows = mx.GetRows();
    unsigned cols = this->GetRows();
    Mx::Matrix<T> temp(rows, cols, 0.0);

    for (unsigned i = 0; i < rows; i++) {
        for (unsigned j = 0; j < cols; j++) {
            for (unsigned k = 0; k < rows; k++) {
                temp(i, j) += (*this)(i, k) * mx(k, j);
            }
        }
    }
    return temp;}

and after every change I make here (or anywhere in the project) I must do some silly delete a letter from statement (int main()) and add it again so VS could include my changes in the class.cpp file...

recnac
  • 3,744
  • 6
  • 24
  • 46
0x07
  • 35
  • 1
  • 5
  • Please take [the tour](https://stackoverflow.com/tour), read [the help page](https://stackoverflow.com/help) and show us the relevant code. Welcome to SO. – Ron Sep 23 '17 at 14:10
  • Welcome to Stack Overflow. Please review [How do I ask a good question](https://stackoverflow.com/help/how-to-ask). Your question should include a clear outline of your *specific* coding-related issue, a summary of what you have already tried and the relevant code in a [Minimal, Complete, and Verifiable example](https://stackoverflow.com/help/mcve), so we have enough information to be able to help. – FluffyKitten Sep 23 '17 at 14:20
  • Templates aren't implemented in a cpp. –  Sep 23 '17 at 14:46
  • Yes, please extend you question. What is relevant to this problem is the ` matrix.hpp` include file if you have one, how it is included from main an from `class.cpp`. But currently what manni66 says appears to be right: implementation of the template should be in header file (unlike normal c++ classes) – rkapl Sep 23 '17 at 15:49

1 Answers1

0

The code you shared is from your class.cpp file? It must all be in a header file, or at least included from a header file, as @manni66 mentioned in a comment on the question. See Why can templates only be implemented in the header file?

Eli
  • 693
  • 5
  • 12