【发布时间】:2012-11-24 18:10:40
【问题描述】:
我的程序有问题。我想创建一个二维容器,我不知道如何实现运算符重载。这是我的代码:
template<class T, int N, int M>//az osztály sablonjai
class my_matrix:public matrix_base<T,N,M>{
public:
my_matrix();
~my_matrix();
T& operator=(my_matrix &rhs);
T& operator()(int rhs,int lhs);
T& operator+(my_matrix<T,N,M> &rhs);
T& operator-(my_matrix<T,N,M> rhs);
T& operator*(my_matrix<T,N,M> rhs);
T& operator/(my_matrix<T,N,M> rhs);
private:
T arr[N][M];//alap kétdimenziós tömb
};
my_matrix<T,N,M>::my_matrix<T,N,M>(){
T arr[N][M];
};
my_matrix<T,N,M>::~my_matrix<T,N,M>(){
delete[] arr;
};
T& operator=(my_matrix rhs){
return this;
};
T& operator()(int rhs,int lhs){
return arr[rhs][lhs];
};
T& my_matrix::operator+(my_matrix<T,N,M> rhs){
return this+lhs;
};
T& my_matrix::operator-(my_matrix<T,N,M> rhs){
return this-lhs;
};
T& my_matrix::operator*(my_matrix<T,N,M> rhs){
return this*lhs;
};
T& my_matrix::operator/(my_matrix<T,N,M> &rhs){
return this/lhs;
};
我想得到这个: 我的矩阵米; 我的矩阵 m2; ... m=m+m2;
matrix_base 是一个未知的抽象类(这是一个任务)。 谁能帮我?谢谢!
【问题讨论】:
-
+ 运算符的代码在哪里?也许这可以指导你。 stackoverflow.com/questions/12625075/…
-
有什么问题?不知道怎么调用对应的基函数?为什么一切都返回
T &?
标签: c++ oop overloading operator-keyword