我的目標是能夠做到這一點:
#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 如下供參考以及錯誤訊息。
嘗試包括:
- 洗掉
template <class U>型別轉換的名稱。 - 洗掉
template <std::size_t N>并更改為const T (&RHS)[]. - 洗掉 [] 迫使我通過
testMat1 = *td1我發現不理想的
1>e:\documents\visual studio 2012\projects\nntest\nntest\rmatrix.h(26): error C3857: 'rMatrix::operator =': 多個模板引數串列是不允許的 1> e:\documents\visual studio 2012\projects\nntest\nntest\rmatrix.h(31) :參見正在編譯的類模板實體 'rMatrix' 的參考 1>e:\documents\visual studio 2012\projects\nntest\nntest\rmatrix.h(102) : 錯誤 C2244: 'rMatrix::operator =' : 無法將函式定義與現有宣告 1> 定義 1> 'rMatrix &rMatrix::operator =(const U (&)[N])' 1> 現有宣告 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 =': 不允許多個模板引數串列 1> with 1> [ 1> T=float 1 >
] 1> e:\documents\visual studio 2012\projects\nntest\nntest\source.cpp(12) :請參閱正在編譯的類模板實體 'rMatrix' 的參考 1> with 1> [ 1> T=float 1> ] 1>e:\documents\visual studio 2012\projects\nntest\nntest\rmatrix.h(26): error C3857: 'rMatrix::operator =': 多個模板引數串列是不允許的 1> with 1> [ 1> T=雙1>
] 1> e:\documents\visual studio 2012\projects\nntest\nntest\source.cpp(13) :請參閱對正在編譯的類模板實體 'rMatrix' 的參考 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 requires a right-hand operation of 'int [4]' (或沒有可接受的轉換) 1> e:\documents\visual studio 2012\projects\nntest\nntest\rmatrix.h(24): 可能是 'rMatrix &rMatrix::operator =(const rMatrix &)' 1> with 1>
[ 1> T=float 1> ] 1> 同時嘗試匹配引數串列 '(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 that take a right-hand 'int [4]' 型別的運算元(或沒有可接受的轉換) 1> e:\documents\visual studio 2012\projects\nntest\nntest\rmatrix.h(24): 可以是 'rMatrix &rMatrix::operator = (const rMatrix &)' 1> with 1>
[ 1> T=double 1> ] 1> 同時嘗試匹配引數串列 '(rMatrix, int [4])' 1> with 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;
}
解決了
#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;
}
uj5u.com熱心網友回復:
Compiler 告訴你第一個問題:不應該有 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。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/312604.html
