Home > Software engineering >  Overloaded assignment operator with arrays
Overloaded assignment operator with arrays

Time:10-07

My goal is to be able to do this:

#include "rMatrix.h"

int main() {    

    rMatrix<double>    testMat1;
    rMatrix<double>    testMat2;

    int td1[] = {1,2,3,4};
    int td2[] = {1,2,3,4};

    testMat1 = td1;
    testMat2 = td2;

}

Unfortunately, my efforts have not panned out. Matrix.h is below for reference as well as the error message.

Attempts include:

  • removing template <class U> designation for type conversion.
  • removing template <std::size_t N> and changing to const T (&RHS)[].
  • removing [] forces me to pass as testMat1 = *td1 which I find to suboptimal

1>e:\documents\visual studio 2012\projects\nntest\nntest\rmatrix.h(26): error C3857: 'rMatrix::operator =': multiple template parameter lists are not allowed 1> e:\documents\visual studio 2012\projects\nntest\nntest\rmatrix.h(31) : see reference to class template instantiation 'rMatrix' being compiled 1>e:\documents\visual studio 2012\projects\nntest\nntest\rmatrix.h(102): error C2244: 'rMatrix::operator =' : unable to match function definition to an existing declaration 1> definition 1> 'rMatrix &rMatrix::operator =(const U (&)[N])' 1> existing declarations 1> 'rMatrix &rMatrix::operator =(const U (&)[N])' 1> 'rMatrix &rMatrix::operator =(const rMatrix &)' 1>e:\documents\visual studio 2012\projects\nntest\nntest\rmatrix.h(26): error C3857: 'rMatrix::operator =': multiple template parameter lists are not allowed 1> with 1> [ 1> T=float 1>
] 1> e:\documents\visual studio 2012\projects\nntest\nntest\source.cpp(12) : see reference to class template instantiation 'rMatrix' being compiled 1> with 1> [ 1> T=float 1> ] 1>e:\documents\visual studio 2012\projects\nntest\nntest\rmatrix.h(26): error C3857: 'rMatrix::operator =': multiple template parameter lists are not allowed 1> with 1> [ 1> T=double 1>
] 1> e:\documents\visual studio 2012\projects\nntest\nntest\source.cpp(13) : see reference to class template instantiation 'rMatrix' being compiled 1> with 1> [ 1> T=double 1> ] 1>e:\documents\visual studio 2012\projects\nntest\nntest\source.cpp(19): error C2679: binary '=' : no operator found which takes a right-hand operand of type 'int [4]' (or there is no acceptable conversion) 1> e:\documents\visual studio 2012\projects\nntest\nntest\rmatrix.h(24): could be 'rMatrix &rMatrix::operator =(const rMatrix &)' 1> with 1>
[ 1> T=float 1> ] 1> while trying to match the argument list '(rMatrix, int [4])' 1> with 1>
[ 1> T=float 1> ] 1>e:\documents\visual studio 2012\projects\nntest\nntest\source.cpp(20): error C2679: binary '=' : no operator found which takes a right-hand operand of type 'int [4]' (or there is no acceptable conversion) 1> e:\documents\visual studio 2012\projects\nntest\nntest\rmatrix.h(24): could be 'rMatrix &rMatrix::operator =(const rMatrix &)' 1> with 1>
[ 1> T=double 1> ] 1> while trying to match the argument list '(rMatrix, int [4])' 1> with 1>
[ 1> T=double 1> ] 1> 1>Build FAILED.

"rMatrix.h"

#include <algorithm>

template <class T>
class rMatrix {

    private:
        
        T* data;
        int colN;
        int rowN;

    public:

        rMatrix();
        rMatrix(unsigned int size);
        rMatrix(int row,int col);
        rMatrix(const rMatrix& mat);
        ~rMatrix();

        T iloc(int index);
        T iloc(int row, int col);

        rMatrix<T>& operator=(const rMatrix & RHS);

        template <class U>
        template <std::size_t N>
        rMatrix<T>& operator=(const U (&RHS)[N]);       
        
        void reshape(int row,int col);
};

template <class T>
rMatrix<T>::rMatrix() : rowN(0), colN(0), data(NULL) {}

template <class T>
rMatrix<T>::rMatrix(const rMatrix<T> & mat) {
    if (data != NULL) {
        data = new T[mat.rowN*mat.colN];
        std::copy(std::begin(mat.data),std::end(mat.data),std::begin(data));
    }
    rowN = mat.rowN;
    colN = mat.colN;
}

template <class T>
rMatrix<T>::rMatrix(unsigned int size) : rowN(size), colN(1) {
    data = new T[size];
    for(unsigned int i = 0; i < size; i  ) {
        data[i] = 0;    
    }
}

template <class T>
rMatrix<T>::rMatrix(int row, int col) : rowN(row), colN(col) {
    unsigned int size = row*col;
    data = new T[size];
    for(unsigned int i = 0; i < size; i  ) {
        data[i] = 0;
    }
    
}

template <class T>
rMatrix<T>::~rMatrix() {
    if (data != NULL) {
        delete [] data;
    }
    data = NULL;
    colN = 0;
    rowN = 0;
}

template <class T>
T rMatrix<T>::iloc(int index) {
    return data[index];
}

template <class T>
T rMatrix<T>::iloc(int row, int column) {
    return this->loc(row   column * rowN);
}

template <class T>
rMatrix<T>& rMatrix<T>::operator=(const rMatrix & RHS) {
    rowN = RHS.rowN;
    colN = RHS.colN;
    data = new T[rowN*colN];
    for(unsigned int i = 0; i < unsigned int(rowN*colN); i  ) {
        data[i] = RHS.data[i];
    }
    //std::copy(std::begin(RHS.data), std::end(RHS.data), std::begin(data));
    return *this;
}

template <class T>             //, std::size_t N>
template <class U>
template <std::size_t N>
rMatrix<T>& rMatrix<T>::operator=(const U (&RHS) [N]) {
    std::copy(std::begin(RHS),std::end(RHS),std::begin(data));
    return *this;
}

Solved

#include <algorithm>

template <class T>
class rMatrix {

    private:
        
        T* data;
        int colN;
        int rowN;

    public:

        rMatrix();
        rMatrix(unsigned int size);
        rMatrix(int row,int col);
        rMatrix(const rMatrix& mat);
        ~rMatrix();

        T iloc(int index);
        T iloc(int row, int col);

        rMatrix<T>& operator=(const rMatrix & RHS);


        template <class U, std::size_t N>
        rMatrix<T>& operator=(const U (&RHS)[N]);       
        
        void reshape(int row,int col);
};

/*** Other Functions ***/    

template <class T>             //, std::size_t N>
template <class U, std::size_t N>
rMatrix<T>& rMatrix<T>::operator=(const U (&RHS) [N]) {
    rowN = N;
    colN = 1;
    data = new T[N];

    for(unsigned int i = 0; i < unsigned int(rowN*colN); i  ) 
        data[i] = static_cast<T>(RHS[i]);   
    
    return *this;
}

CodePudding user response:

The Compiler is telling you about the first issue: there should not be 2 template lines, if you want U and N it needs to look like this:

template <typename U, std::size_t N>
rMatrix<T>& operator=(const U (&RHS)[N]) ;

However, to simplify try this first:

template < std::size_t N>
rMatrix<T>& operator=(const int (&RHS)[N]) ;

I think this will not work anyway. I believe arrays are passed as pointers, even if you declare the parameter with a size. But try it anyway, i might be mistaken. If i'm right, then you're out of luck. You need to expect the values as a pointer with an additional count parameter, or switch to std::array.

  • Related