我班上的运算子=有问题。这是简化的代码:
班级:
#include <iostream>
using namespace std;
class Mat{
private:
int *m1;
int *m2;
unsigned rows;
unsigned cols;
void write(unsigned r_max, unsigned c_max){
m1 = new int [r_max*c_max];
for(unsigned i = 0; i < r_max*c_max; i++){
m1[i] = i;
}
}
public:
Mat():
rows(1), cols(1){
m1 = new int [1];
m1[0] = 0;
m2 = new int [1];
m2[0] = 0;
}
Mat(unsigned r, unsigned c):
rows(r), cols(c){
write(rows, cols);
m2 = new int [rows*cols];
for(unsigned i = 0; i < rows*cols; i++)
m2[i] = 0;
}
Mat &operator=(const Mat &w){
int *new_ptr1 = NULL;
new_ptr1 = new int [w.rows*w.cols];
int *new_ptr2 = NULL;
new_ptr2 = new int [w.rows*w.cols];
for(unsigned i = 0; i < w.rows*w.cols; i++){
new_ptr1[i] = w.say_m1(i);
new_ptr2[i] = w.say_m2(i);
}
delete[] w.m1;
delete[] w.m2;
m1 = new_ptr1;
m2 = new_ptr2;
rows = w.rows;
cols = w.cols;
return *this;
}
int say_m1(unsigned i) const{ return m1[i]; }
int say_m2(unsigned i) const{ return m2[i]; }
~Mat(){
delete[] m1;
delete[] m2;
}
};
这是我主要需要做的:
#include "Mat.cpp"
#include <iostream>
#include <cstdlib>
using namespace std;
int main(int argc, char **argv){
Mat a;
Mat b(20, 20);
a = b;
return 0;
}
我认为问题出在我重载运算符的类中=我不知道如何将正确的对象数据复制到左侧的对象数据中,我不确定是否正确删除...
我认为问题出在我重载运算符的类中=我不知道如何将正确的对象数据复制到左侧的对象数据中,我不确定是否正确删除...
您没有(正确删除)。
运算符=应该更改的值*this
。而是删除w.m1和w.m2(其中w是您的参数)。
更正的代码:
Mat &operator=(const Mat &w){
int *new_ptr1 = NULL;
new_ptr1 = new int [w.rows*w.cols];
int *new_ptr2 = NULL;
new_ptr2 = new int [w.rows*w.cols];
for(unsigned i = 0; i < w.rows*w.cols; i++){
new_ptr1[i] = w.say_m1(i);
new_ptr2[i] = w.say_m2(i);
}
delete[] m1; // <<< HERE
delete[] m2; // <<< HERE
m1 = new_ptr1;
m2 = new_ptr2;
rows = w.rows;
cols = w.cols;
return *this;
}
也就是说,您的代码还有很多其他问题:
假设您编写了这段代码来学习如何在一个类中操作动态数组:
您正在使用直接管理两个不同分配资源的类。您应该考虑在m1和m2上使用智能指针(出于安全考虑)。
实施以下三个规则:构造函数(您已经有了这个),复制构造函数(您没有这个)和赋值运算符(您已经有了这个)。
向您的班级添加析构函数。
**假设这是您将要使用的代码,而不是为学习数组而编写的代码:**
[]将std::vector
s用于m1和m2。这将使您不必执行三和析构函数的规则。
您using namespace std;
在全球范围内使用。不要这样做,因为它会带来很多问题。
编辑:
更好的代码:
#include <iostream>
#include <vector>
class Mat{
std::vector<int> life;
std::vector<int> neighborhood;
public:
// this is unnecessary
// void write(unsigned r_max, unsigned c_max); {
Mat() : life(1), meighborhood(1) // fill each with 1 int with default value (0)
{
}
Mat(unsigned r, unsigned c)
: life(r), meighborhood(c) // fill each with r(and c) ints with default value (0)
{
}
// ~Mat() became unnecessary: destructors of std::vector will deallocate fine
Mat &operator=(const Mat &w) {
// create replacements before doing changing any value
// this way, if you get an exception while creating the data
// the value in the obhect does not change
std::vector<int> new_life(w.life);
std::vector<int> new_neighborhood(w.neighborhood);
life.swap(new_life);
neighborhood.swap(new_neighborhood);
return *this;
}
// use std::vector<int>::at which throws an exception if the index is invalid
// if you are not interested in the validation of the index
// return life[i] and neighborhood[i]
int say_m1(unsigned i) const{ return life.at(i); }
int say_m2(unsigned i) const{ return neighborhood.at(i); }
};
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句