矩阵运算符不起作用+超载
我正在尝试制作一个示例(只是示例!我知道它会泄漏)应用程序来学习 C++ 中的运算符重载,但我得到总和元素的零值......我怀疑问题在于 int复制构造函数。
具体实现如下:
class Matrix{
public:
Matrix(int row, int col);
Matrix(const Matrix& src);
float& set(int row, int col);
float get(int row, int col);
const Matrix & operator+(const Matrix& rhs);
private:
float* data;
int nrow;
int ncol;
};
Matrix::Matrix(int row, int col){
nrow = row;
ncol = ncol;
data = new float[nrow*ncol];
}
Matrix::Matrix(const Matrix& src){
nrow = src.nrow;
ncol = src.ncol;
data = new float[nrow*ncol];
for(int i = 0; i < nrow*ncol; i++){
data[i] = src.data[i];
}
}
float& Matrix::set(int row, int col){
return data[row*ncol+col];
}
float Matrix::get(int row, int col){
return data[row*ncol+col];
}
const Matrix & Matrix::operator+(const Matrix& rhs){
if (this->nrow == rhs.nrow && this->ncol == rhs.ncol){
Matrix* m = new Matrix(rhs.nrow, rhs.ncol);
for(int i=0; i< nrow*ncol; i++){
m->data[i] = data[i] + rhs.data[i];
}
return *m;
} else {
throw -1;
}
}
#include <iostream>
using namespace std;
int main ()
{
Matrix A(1,1);
Matrix B(1,1);
A.set(0,0)=1;
B.set(0,0)=2;
cout << A.get(0,0) << endl;
cout << B.get(0,0) << endl;
Matrix C = A + B; // Marix C(A+B);
cout << C.get(0,0) << endl;
return 0;
}
I'm trying to make a example (just example! I know it leaks) app to learn operator overloading in C++, but I get the value of zero to the elements of the sum.... I suspect that the problem is int the copy constructor.
The Detailed implementation is below:
class Matrix{
public:
Matrix(int row, int col);
Matrix(const Matrix& src);
float& set(int row, int col);
float get(int row, int col);
const Matrix & operator+(const Matrix& rhs);
private:
float* data;
int nrow;
int ncol;
};
Matrix::Matrix(int row, int col){
nrow = row;
ncol = ncol;
data = new float[nrow*ncol];
}
Matrix::Matrix(const Matrix& src){
nrow = src.nrow;
ncol = src.ncol;
data = new float[nrow*ncol];
for(int i = 0; i < nrow*ncol; i++){
data[i] = src.data[i];
}
}
float& Matrix::set(int row, int col){
return data[row*ncol+col];
}
float Matrix::get(int row, int col){
return data[row*ncol+col];
}
const Matrix & Matrix::operator+(const Matrix& rhs){
if (this->nrow == rhs.nrow && this->ncol == rhs.ncol){
Matrix* m = new Matrix(rhs.nrow, rhs.ncol);
for(int i=0; i< nrow*ncol; i++){
m->data[i] = data[i] + rhs.data[i];
}
return *m;
} else {
throw -1;
}
}
#include <iostream>
using namespace std;
int main ()
{
Matrix A(1,1);
Matrix B(1,1);
A.set(0,0)=1;
B.set(0,0)=2;
cout << A.get(0,0) << endl;
cout << B.get(0,0) << endl;
Matrix C = A + B; // Marix C(A+B);
cout << C.get(0,0) << endl;
return 0;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
其中存在一个拼写错误,导致您的代码具有未定义的行为。
修复它:(
确保将编译器警告/诊断级别调到最大,GCC 会捕获此问题。)
您也泄漏了
float[]
,因此您的代码不完整。不要忘记添加适当的析构函数,并始终遵循三原则 。There's a typo in there that results in your code having undefined behavior.
Fix it with:
(Make sure you turn you compilers warning/diagnostics level to the maximum, GCC catches this.)
You're leaking your
float[]
s too, so your code isn't complete. Don't forget to add proper destructors, and always follow the rule of three.