分段故障过载 (<<)
Segmentation fault overloading ostream (<<)
,所以我正在练习C 的编码,并且我正在尝试编写具有相关超载操作的矩阵(存储为数组)的类。
我已经定义了班级,并试图超负荷&lt;&lt;运算符,但我当前的代码正在导致分割故障(i在Ubuntu中使用G 编译)。我在网上看,遇到类似问题的人总是会忘记在其过载功能中返回OS,但是我已经这样做了,所以我不知道我的问题可能是什么。此外,我的超载操作员在导致分割故障之前多次工作。
任何帮助将不胜感激。
这是我的代码:
#include<iostream>
#include<stdlib.h> // for c style exit
using namespace std;
class matrix
{
// Friends
friend ostream & operator<<(ostream &os, const matrix &mat);
friend istream & operator>>(istream &is, matrix &mat);
private:
double *mdata;
int rows,columns;
public:
// Default constructor
matrix(){mdata=0; rows=columns=0;}
// Parameterized constructor
matrix(int m, int n){mdata = new double[ m*n ]; rows = m; columns = n;}
// Copy constructor
matrix(matrix &mat)
// Destructor
~matrix(){delete[] mdata; cout<<"Destructing array."<<endl;}
// Access functions
int getrows() const {return rows;} // Return number of rows
int getcols() const {return columns;} // Return number of columns
int index(int m, int n) const // Return position in array of element (m,n)
{
if(m>0 && m<=rows && n>0 && n<=columns) return (n-1)+(m-1)*columns;
else {cout<<"Error: out of range"<<endl; exit(1);}
}
double & operator()(int m, int n)const {return mdata[index(m,n)];}
// Other access functions go here
double & operator[](int i) {return mdata[i];}
// Other functions
// Copy Assignment operator
matrix & operator=(matrix &mat);
};
// Member functions defined outside class
matrix::matrix(matrix &mat){
rows = mat.getrows();
columns = mat.getcols();
for(int j = 0; j<rows*columns; j++){mdata[j] = mat[j];}
}
matrix & matrix::operator=(matrix &mat){
if (&mat == this) return *this;
delete[] mdata; rows = 0; columns = 0;
rows = mat.getrows(); columns = mat.getcols();
if(rows>0&&columns>0){
mdata = new double[(columns-1) + (rows-1)*columns + 1];
for(int j = 0; j<rows*columns; j++){mdata[j] = mat[j];}
}
return *this;
}
// Overload insertion to output stream for matrices
ostream & operator<<(ostream &os, const matrix &mat){
for(int j = 0;j<mat.rows;j++){
for(int k = 0;k<mat.columns;k++){
os << mat(j+1,k+1) << " ";
}
os << endl;
}
return os;
}
// Main program
int main(){
// Demonstrate default constructor
matrix a1;
cout<<a1;
// Parameterized constructor
const int m(2),n(2);
matrix a2(m,n);
// Set values for a2 here
a2[0] = 1; a2[1] = 2; a2[2] = 3; a2[3] = 4;
// Print matrix a2
cout<<a2;
// Deep copy by assignment: define new matrix a3 then copy from a2 to a3
matrix a3(m,n);
cout<<a3;
a3=a2;
cout<<a3;
// Modify contents of original matrix and show assigned matrix is unchanged here
a2[0] = 5;
cout<<a2;
cout<<a3; //here is where segmentation fault occurs
return 0;
}
看来您超出了矩阵的限制。您应该删除i
和j
...
+1
for(int j = 0;j<mat.rows;j++){
for(int k = 0;k<mat.columns;k++){
os << mat(j,k) << " ";
}
当数组具有3个元素时,这意味着您可以访问以下3个元素:
array[0]
array[1]
array[2]
但不是array[3]
,这是您的数组的第四个元素,其长度为3。
作为经验法则,当您获得segmentation fault
时,应使用gdb
或valgrind
运行程序。这些工具通常会为您提供非常有价值的信息,以发现代码中内存访问错误的根本原因。
您需要使该函数成为类矩阵的friend
,因为它无法访问columns
或rows
,因为它们是私人成员。典型的错误operator<<
和operator>>
。永远不要忘记friend
。
相关文章:
- 分段故障(堆芯转储)矢量
- C++中的动态铸造故障
- 数组的指针从不分段故障
- 请解释这句话(cout<<1+int((a<b)^((b-a)&1) )<<endl
- 呼叫运营商<<临时
- vscode g++链路故障:体系结构x86_64的未定义符号
- 访问被拒绝后,c++中的故障保护代码
- Windows 10-使用gtkmm-3.0库和g++[包括再现]的分段故障
- 如何防止clang格式在流运算符调用之间添加换行符<<
- <<操作员在下面的行中工作
- 调试 CUDA MMU 故障
- Geeksforgeeks C 程序故障排除:IEE 754 表示法为十进制
- Arch Linux.AUR 包 mysql 不能用 makepkg 构建.错误:构建 () 中出现故障
- 正在处理故障(堆芯转储)
- 在 Boost::fiber 中引发的BOOST_ASSERT故障 Visual Studio "Debug" 构建
- 如何进行故障排除:未定义对"非虚拟 thunk to ..."的引用
- C++函数过载会导致 SEG 故障
- 分段故障 运行C++代码时出现 SIGSEGV
- 当新的故障时,是否有必要留出一些紧急内存?
- Python & C-C++ 扩展模块案例段故障