帶數(shù)組的重載賦值運算符

我的目標是能夠做到這一點:

#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是下面的參考以及錯誤消息。

Attempts include:

  • 刪除類型轉(zhuǎn)換的template <class U>名稱。
  • 刪除template <std::size_t N>并更改為const T (&RHS)[]
  • 刪除[]會迫使我以testMat1 = *td1的形式通過,我發(fā)現(xiàn)這是次優(yōu)的

1> e:\documents\visual studio2012\projects\nntest\nntest\rmatrix.h(26):錯誤C3857:'rMatrix::operator=':不允許使用多個模板參數(shù)列表1>e:\documents\visual studio2012\projects\nntest\nntest\rmatrix.h(31):請參閱對正在編譯的類模板實例化'rMatrix'的引用1>e:\documents\visual studio2012\projects\nntest\nntest\rmatrix.h(102):錯誤C2244:'rMatrix::operator=':無法將函數(shù)定義與現(xiàn)有聲明1>定義1>'rMatrix&rMatrix::operator=(常數(shù)U(&)[N])'1>現(xiàn)有聲明1>'rMatrix&rMatrix::operator=(常數(shù)U(&)[N])'1>'rMatrix&rMatrix::operator=(常數(shù)rMatrix&)'1>e:\documents\visual studio2012\projects\nntest\nntest\rmatrix.h(26):錯誤C3857:'rMatrix::operator=':不允許使用1>[1>T=float 1>]1>e:\documents\visual studio2012\projects\nntest\nntest\source.cpp(12):請參閱使用1>[1>T=float 1>]1>e:\documents\visual studio2012\projects\nntest\nntest\rmatrix.h(26)編譯的類模板實例化'rMatrix'的參考:錯誤C3857:'rMatrix::operator=':不允許使用1>[1>T=double 1>]1>e:\documents\visual studio2012\projects\nntest\nntest\source.cpp(13):請參閱正在使用1>[1>T=double 1>]1>e:\documents\visual studio2012\projects\nntest\nntest\source.cpp(19)編譯的類模板實例化'rMatrix'的引用:錯誤C2679:二進制“=”:未找到接受類型為“int[4]”的right-hand操作數(shù)的運算符(或沒有可接受的轉(zhuǎn)換)1>e:\documents\visual studio2012\projects\nntest\nntest\rmatrix.h(24):可能是“rMatrix&rMatrix::operator=(const-rMatrix&)'1>和1>[1>T=float 1>]1>,同時嘗試將參數(shù)列表“(rMatrix,int[4])'1>與1>匹配[1>T=float 1>]1>e:\documents\visual studio2012\projects\nntest\nntest\source.cpp(20):錯誤C2679:二進制“=”:未找到接受類型為“int[4]”的right-hand操作數(shù)的運算符(或沒有可接受的轉(zhuǎn)換)1>e:\documents\visual studio2012\projects\nntest\nntest\rmatrix.h(24):可以是“rMatrix&rMatrix::operator=(const-rMatrix&)”1>,帶有1>[1>T=double 1>]1>嘗試將參數(shù)列表“(rMatrix,int[4])”1>與1>[1>T=double 1>]1>1>生成匹配時失敗。

"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;
}
? 最佳回答:

編譯器將告訴您第一個問題:不應(yīng)該有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]) ;

我認為這無論如何都行不通。我相信數(shù)組是作為指針傳遞的,即使您用一個大小聲明參數(shù)也是如此。但無論如何試試看,我可能弄錯了。如果我是對的,那你就不走運了。您需要將這些值作為帶有附加計數(shù)參數(shù)的指針,或者切換到std::array。

主站蜘蛛池模板: 日本免费一区二区三区四区五六区| 在线精品视频一区二区| 性色AV 一区二区三区| 国产主播在线一区| 无码av免费毛片一区二区| 亚洲av日韩综合一区二区三区| 精品视频在线观看一区二区 | 国产一区二区三区在线免费| asmr国产一区在线| 午夜性色一区二区三区不卡视频| 99精品国产一区二区三区不卡| 久久综合精品国产一区二区三区| 在线日韩麻豆一区| 日韩a无吗一区二区三区| 无码日本电影一区二区网站| 末成年女AV片一区二区| 色偷偷久久一区二区三区| 国产精品视频第一区二区三区| 成人精品一区二区三区中文字幕 | 中日av乱码一区二区三区乱码| 在线日韩麻豆一区| 无码精品久久一区二区三区 | 国产亚洲综合一区柠檬导航| 精品一区二区三区免费观看| 精品人体无码一区二区三区 | 国产日韩视频一区| 国产免费一区二区三区VR| 理论亚洲区美一区二区三区| 怡红院一区二区在线观看| 无码少妇A片一区二区三区 | 久久无码AV一区二区三区| 久久国产免费一区| 五月婷婷一区二区| 色综合视频一区中文字幕| 日本人的色道www免费一区| 国产中的精品一区的| 久久精品国产一区二区| 国产精品日本一区二区不卡视频 | 精品女同一区二区三区在线| 无码精品人妻一区二区三区影院 | 国产中的精品一区的|