重载>>输入矩阵(类)的函数不起作用
Function to overload >> to input a matrix (class) isn't working
所以,我已经写好了这个类,它看起来有点像这样:
class matrix
{
// Friends
friend ostream & operator << (ostream &os, const matrix &mat);
friend istream & operator >> (istream &is, matrix &mat);
private:
double *mdata;
int rows, columns, size;
之后我又写了:
// Assignment operator
public:
matrix & operator=(const matrix &m) {
if(&m == this) {
return *this; // no self assignment
}
// First delete this object's array
delete[] mdata;
columns=0;
rows=0;
mdata=0;
int size=0;
// Now copy size and declare new array
size=m.getcols()*m.getrows();
if(size>0) {
mdata=new double[size];
// Copy values into new array
for(int i=0;i<size;i++) {
mdata[i] = m.mdata[i];
}
}
columns = m.getcols();
rows = m.getrows();
return *this; // Special pointer
}
类外还有这个
ostream & operator << (ostream &os, const matrix &mat) {
// Code goes here
os << "n";
int j = 1;
for (int i=0; i < mat.size; i++) {
os << mat.mdata[i] << " ";
if (i+1 == j*mat.getcols()) {
os << "n";
j = j + 1;
}
}
os << "n";
os << "Wolfram|Alpha code:n[[";
j = 1;
for (int i=0; i < mat.size; i++) {
os << mat.mdata[i];
if (i+1 != j*mat.getcols()){
os << ",";
}
if (i+1 == j*mat.getcols()) {
if (i+1 != mat.size) {
os << "],[";
}
else {
os << "]";
}
j = j + 1;
}
}
os << "] n";
return os;
}
istream & operator >> (istream &is, matrix &mat) {
is >> mat.rows >> mat.columns;
int size(mat.rows*mat.columns);
if(size>0) {
cout << "Enter " << size << " values (top row, second row... last row - left to right)" << endl;
mat.mdata=new double[size];
// Copy values into new array
for(int i=0;i<size;i++) {
is >> mat.mdata[i];
}
}
return is;
}
但是当运行代码时(在main中):
cout << "Enter the rows and columns of a custom Matrix, row then column:" << endl;
matrix custom;
cin >> custom;
cout << custom;
cout << custom.getrows() << endl;
我没有得到任何值打印出来…
Enter the rows and columns of a custom Matrix, row then column:
Default matrix constructor called
2
2
Enter 4 values (top row, second row... last row - left to right)
1 2 3 4
Wolfram|Alpha code:
[[]
2
Destructor called
你知道我做错了什么吗?完整代码在这里
编辑:忘了说,我包含赋值操作符是因为它有相同(或类似)的问题。
您从未在operator>>
过载中更新mat.size
,因此尽管您读取了4个值,但矩阵认为它是空的并且没有打印出任何内容。
同样,非常重要的是,如果传递给operator>>
过载的矩阵已经有数据,那么您将泄漏内存,因为您没有释放该数据,并且您将新指针分配给mat.mdata
你可以这样做:
istream & operator >> (istream &is, matrix &mat) {
int rows, columns;
is >> rows >> columns;
if (!is)
return is; // couldn't read rows and cols
matrix tmp(rows, columns);
if(tmp.size>0) {
cout << "Enter " << tmp.size << " values (top row, second row... last row - left to right)" << endl;
注意:这里不要分配新数组,这是在上面的构造函数中完成的。
// Copy values into new array
for(int i=0;i<tmp.size;i++) {
is >> tmp.mdata[i];
}
}
if (is) // read all values successfully
mat = tmp;
这要求你的赋值操作符正确工作,所以现在是确保这一点的好时机!另一个选项是交换:
mat.swap(tmp);
这需要一个正确的matrix::swap(matrix&)
成员函数,这是一个有用的东西,有很多原因。
return is;
}
请注意,我加入了错误检查,以确保您实际从流中读取预期的数据。
赋值操作符的错误在这里:
int size=0;
// Now copy size and declare new array
size=m.getcols()*m.getrows();
声明一个新的局部变量size
,并更新该变量。这意味着this->size
永远不会更新。您不想声明一个新的size
,您只想更新成员变量,因此将上述更改为:
// Now copy size and declare new array
size=m.getcols()*m.getrows();
相关文章:
- 我的神经网络不起作用 [XOR 问题]
- 在C++程序中输入的文本文件将不起作用,除非文本被复制和粘贴
- C++映射:具有自定义类的运算符[]不起作用(总是返回0)
- C++为什么尽管我调用了void函数,它却不起作用
- 为什么在保护模式下继承升级不起作用
- 循环在计数器中不起作用
- 在其他文件中创建类时在 c++ 项目中不起作用
- Visual studio代码重构似乎不起作用(例如,重命名符号-f2)
- 为什么二进制搜索在我的测试中不起作用
- 我的代码中有错误吗?使用BGI图形的C++代码对我不起作用
- 为什么 const std::p air<K,V>& 在 std::map 上基于范围的 for 循环不起作用?
- 带有指定长度字符* 参数的 std::regex_search 在 VS2017 中不起作用?
- Bjarne Stroustrup Book - std_lib_facilities.h - 不起作用(未知类型名称)
- 为什么简单的算术减法在"if"条件下不起作用?
- 为什么Stroustup书中的has_f不起作用
- 你能检查一下为什么在这个代码中从链接列表中删除项目不起作用吗
- 嵌套While循环不起作用(C++问题)
- C++Matching Brackets 2解决方案不起作用
- 为什么这段代码不起作用,我该如何解决?
- 我正在开发服务器,ip作为参数传递不起作用