我的目標是能夠做到這一點:
#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,它需要如下所示:
但是,要簡化,請先嘗試以下操作:
我認為這無論如何都行不通。我相信數(shù)組是作為指針傳遞的,即使您用一個大小聲明參數(shù)也是如此。但無論如何試試看,我可能弄錯了。如果我是對的,那你就不走運了。您需要將這些值作為帶有附加計數(shù)參數(shù)的指針,或者切換到std::array。