c++ - 重载运算符中的段错误>>

标签 c++ segmentation-fault overloading

我正在尝试为正交链接稀疏矩阵编写代码。

问题是:
稀疏矩阵的另一种链接表示使用具有字段 down、right、row、col 和 value 的节点。表示稀疏矩阵的每个非零项 由一个节点。零项未明确存储。节点链接在一起形成两个循环列表。第一个列表,即行列表,由使用右字段按行链接节点并在行内按列链接节点组成。第二个列表,列列表,由通过向下字段链接节点组成。在此列表中,节点按列链接,并在列内按行链接。这两个列表共享一个公共(public)头节点。此外,节点被添加到矩阵的维度。

输入文件如下所示:

//矩阵A

4 4 7
1 1 2
1 4 1
2 2 7
3 1 9
3 3 8
4 2 4
4 3 5

//矩阵 B

4 4 5
1 3 4
2 1 6
2 3 3
3 2 5
4 4 9

这是我的运算符(operator)>:

istream& operator>>(istream& in, OrthogonalLinkedSparseMatrix& x){

    in >> x.numRows >> x.numCols >> x.numTerms;
    in >> x.currentNode->row >> x.currentNode->col >> x.currentNode->value;
    x.push_back(x.currentNode);
    if((x.currentNode->row == 1)&&(x.currentNode->col == 1)){

        x.hnode->right = x.currentNode;
        x.hnode->down = x.currentNode;
    }
    if(x.currentNode->col == 1){
        x.hnode->down = x.currentNode;
    }
    if(x.currentNode->row == 1){
        x.hnode->right = x.currentNode;
    }

    for (int i = 2; i <= x.numTerms; i++) {

        in >> x.currentNode->row >> x.currentNode->col >> x.currentNode->value;

        x.push_back(x.currentNode);

    }


    return in;

}

它编译得很好。但是当我尝试运行它时,我不断收到段错误。
谁能帮忙?? 非常感谢!

这是 OrthogonalLinkedSparseMatrix.h:

#ifndef O_L_SPARSE_MATRIX_H

#define O_L_SPARSE_MATRIX_H



#include <iostream>

#include <fstream>

#include "node.h"

#include "myExceptions.h"



using namespace std;



class OrthogonalLinkedSparseMatrix;

ostream& operator<< (ostream&, OrthogonalLinkedSparseMatrix&);

istream& operator>> (istream&, OrthogonalLinkedSparseMatrix&);



class OrthogonalLinkedSparseMatrix{

public:

    friend ostream& operator<<(ostream& out, OrthogonalLinkedSparseMatrix& x);

    friend istream& operator>>(istream& in, OrthogonalLinkedSparseMatrix& x);

    OrthogonalLinkedSparseMatrix(){}

    ~OrthogonalLinkedSparseMatrix(){}

    void transpose(OrthogonalLinkedSparseMatrix &b);

    void add(OrthogonalLinkedSparseMatrix &a, OrthogonalLinkedSparseMatrix &c);

    void push_back(matrixNode *&mat_Node);

    void setDowns(matrixNode *&mat_Node);

private:

    matrixNode *hnode;

    int numRows, numCols, numTerms;

    matrixNode *currentNode;

    matrixNode *previousNode;

    matrixNode *nextNode;

};

 // code for operator >> & <<, etc goes here, but everything's commented out except operator >>  

#endif

编辑:我也包括操作符<<:

    ostream& operator<<(ostream& out, OrthogonalLinkedSparseMatrix& x){

    if(x.numTerms == 0){

        out << "No non-zero terms" << endl;

        return out;

    }

    out << x.numRows << x.numCols << x.numTerms << endl;

    for (int i = 0; i < x.numTerms; i++) {

        out << x.currentNode->row << x.currentNode->col << x.currentNode->value << endl;

    }

    return out;

}

最佳答案

我认为您的问题出在 currentNode 上。你不应该需要它作为一个类变量,而是你应该在每次从流中读取输入时创建一个新的。例如,

istream& operator>>(istream& in, OrthogonalLinkedSparseMatrix& x)
{
    in >> x.numRows >> x.numCols >> x.numTerms;

    int inRow, inCol, inValue;
    in >> inRow >> inCol >> inValue;         // Get the values from input

    // note: this allocates a NEW matrixNode on the heap, and pushes a pointer into the matrix.
    x.push_back(new matrixNode(inRow, inCol, inValue));

    if(x.currentNode->col == 1){
        x.hnode->down = x.currentNode;
    }
    if(x.currentNode->row == 1){
        x.hnode->right = x.currentNode;
    }

    for (int i = 2; i <= x.numTerms; i++) 
    {
        in >> inRow >> inCol >> inValue;         // Get the values from input

        // note: this allocates a NEW matrixNode on the heap, and pushes a pointer into the matrix.
        x.push_back(new matrixNode(inRow, inCol, inValue));
    }

    return in;
}

这里有几点需要注意:

  • 每次调用 push_back() 时,都会插入一个新的 matrixNode。在您的实现中,总是添加相同的节点,并且可能从未初始化过。
  • 我假设 matrixNode 有一个带有 3 个参数的构造函数。这应该很容易添加。

一般来说,自己管理指针是非常危险的,容易出现内存泄漏。在这种情况下,当您不再需要每个指针时调用 delete 很重要。您很可能希望在析构函数中执行此操作。

编辑: 看起来 hnode 可能与无效指针一起使用。确保在使用之前分配/创建此 matrixNode 指针。

关于c++ - 重载运算符中的段错误>>,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5135100/

相关文章:

c++ - 由于编译时计算值,如何避免 "unreachable statement"?

c - 在内存中执行 2D 数组表示时会导致段错误 : 11

c++ - 调用派生类的虚函数时出现段错误

java - 警告 : [overloads] method m1 is potentially ambiguous with method m2

c++ - C++二分查找中的比较仿函数

c++程序在输入时跳过行[控制台应用程序]

c++ - 从模板中的类获取类型

c - 段错误双指针

c++ - 调用了错误的重载模板函数

Python:在这种情况下是否需要 isinstance()?