【发布时间】:2021-11-27 02:46:51
【问题描述】:
我的目标是能够做到这一点:
#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):错误 C3857: 'rMatrix::operator =': 没有多个模板参数列表 允许 1> e:\documents\visual studio 2012\projects\nntest\nntest\rmatrix.h(31) :参见类参考 模板实例化“rMatrix”正在编译 1>e:\文档\视觉工作室 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):错误 C3857: 'rMatrix::operator =': 没有多个模板参数列表 允许 1> with 1> [ 1> T=float 1>
] 1> e:\documents\visual studio 2012\projects\nntest\nntest\source.cpp(12) :参见类参考 模板实例化 'rMatrix' 正在编译 1> 和 1> [ 1> T=float 1> ] 1>e:\documents\visual studio 2012\projects\nntest\nntest\rmatrix.h(26):错误 C3857: 'rMatrix::operator =': 没有多个模板参数列表 允许 1> with 1> [ 1> T=double 1>
] 1> e:\documents\visual studio 2012\projects\nntest\nntest\source.cpp(13) :参见类参考 模板实例化 'rMatrix' 正在编译 1> 和 1> [ 1> T=double 1> ] 1>e:\documents\visual studio 2012\projects\nntest\nntest\source.cpp(19):错误 C2679:二进制“=”: 未找到采用“int [4]”类型的右手操作数的运算符 (或者没有可接受的转换) 1> e:\documents\visual 工作室 2012\projects\nntest\nntest\rmatrix.h(24): 可能是 'rMatrix &rMatrix::operator =(const rMatrix &)' 1> with 1>
[ 1> T=float 1> ] 1> 试图 将参数列表 '(rMatrix, int [4])' 1> 与 1>
匹配 [ 1> T=float 1> ] 1>e:\documents\visual studio 2012\projects\nntest\nntest\source.cpp(20):错误 C2679:二进制“=”: 未找到采用“int [4]”类型的右手操作数的运算符 (或者没有可接受的转换) 1> e:\documents\visual 工作室 2012\projects\nntest\nntest\rmatrix.h(24): 可能是 'rMatrix &rMatrix::operator =(const rMatrix &)' 1> with 1>
[ 1> T=double 1> ] 1> 试图 将参数列表 '(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;
}
已解决
#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;
}
【问题讨论】:
-
template <class U>的目的是什么?尤其是当您的声明是rMatrix<T>& operator=(const T (&RHS)[N]);-U未使用时。 -
糟糕,正在更改一堆内容,但在发布之前忘记将其切换回来。呵呵,对不起。修复。在第二点上,我认为它的目的是按照我的想法完成一些 Matrix
= int[] 的事情。也许我让它变得比需要的更难。
标签: arrays class c++11 templates operator-overloading