带数组的重载赋值运算符

Overloaded assignment operator with arrays

我的目标是能够做到这一点:

#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;

}

不幸的是,我的努力没有成功。 Matrix.h 下面是参考和错误信息。

尝试包括:

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;
}

已解决

#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;
}

编译器告诉你第一个问题:不应该有 2 个模板行,如果你想要 U 和 N,它需要看起来像这样:

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

但是,为了简化,请先尝试这个:

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

我认为这无论如何都行不通。我相信数组作为指针传递,即使您用大小声明参数。但无论如何尝试一下,我可能会弄错。 如果我是对的,那你就倒霉了。您需要将这些值作为带有附加计数参数的指针,或者切换到 std::array.