c++ - 取消引用指向其类内对象的指针

标签 c++ oop pointers

我有SquareMatrix如此定义

方阵.h

#ifndef SQUAREMATRIX_H
#define SQUAREMATRIX_H
#include "Matrix.h"
#include <vector>

class SquareMatrix : public Matrix
{
    public:
        SquareMatrix();
        SquareMatrix(std::vector<std::vector<long double> >);
        //~SquareMatrix();    //just in case there is dynamic memory explicitly used
        //convenient member functions exclusive to SquareMatrix
        bool isUpperDiagonalMatrix() const;
        static SquareMatrix identityMatrix(unsigned);
        void LUDecompose();
        SquareMatrix *Lptr, *Uptr, *Pptr; //should be initialized by LUDecompose before using
    protected:
        void validateData();
    private:

};

#endif // SQUAREMATRIX_H

我正在尝试设置 Lptr , Uptr (可能还有 Pptr )调用 SquareMatrix::LUDecompose() .定义如下:

void SquareMatrix::LUDecompose()
{
    unsigned rowCount = this->getRowCount();
    //initialize L to identityMatrix
    *this->Lptr = SquareMatrix::identityMatrix(rowCount);
    //initialize U to sparse matrix with the first row containing the sole non-zero elements
    std::vector<std::vector<long double> > UData(1, this->matrixData[0]);   //making first rowVector the first rowVector of this
    UData.insert(UData.end(), rowCount - 1, std::vector<long double>(rowCount,0)); //making all other rowVectors zero vectors
    *Uptr = SquareMatrix(UData);
    // attempting to perform LU decomposition
    for (unsigned j = 0; j < rowCount; j++)
    {
        long double pivot = Uptr->matrixData[j][j];
        //the pivot should be non-zero; throwing exception that should effectively cause function to return
        if (pivot == 0)
            throw MatrixArithmeticException(LU_DECOMPOSITION_FAILURE);
        for (unsigned k = j+1; k < rowCount; k++)
        {
            if (j == 0)
            {
                //using *this to compute entries for L,U
                this->Lptr->matrixData[k][j] = (this->matrixData[k][j])/pivot;   //setting columns of L
                long double multiplier = this->Lptr->matrixData[k][j];
                //setting row of U
                for (unsigned l = k; l < rowCount; l++)
                {
                    Uptr->matrixData[k][l] = (this->matrixData[k][l])-multiplier*(this->matrixData[0][l]);
                }
            }
            else
            {
                //using U to compute entries for L,U
                //same procedure as before
                this->Lptr->matrixData[k][j] = (Uptr->matrixData[k][j])/pivot;
                long double multiplier = this->Lptr->matrixData[k][j];
                for (unsigned l = k; l < rowCount; l++)
                {
                    Uptr->matrixData[k][l] -= multiplier*(Uptr->matrixData[0][l]);
                }
            }
        }
    }
}

在尝试测试此功能时,它向我抛出一个段错误,最后一行是我尝试操作 Lptr 的第一行.

我尝试更改 Lptr 指向的对象 ,我知道我将无法引用该函数并将指针设置为等于该引用。换句话说,我的编译器(GNU GCC 编译器)不允许 this->Lptr = &SquareMatrix::identityMatrix(rowCount);因为它会抛出 -fpermissive 类型错误。

备注:SquareMatrix::identityMatrix(unsigned)定义为:

SquareMatrix SquareMatrix::identityMatrix(unsigned size)
{
    std::vector<long double> rowVector(size, 0L);
    std::vector<std::vector<long double> > identityMatrixData;
    for (int i = 0; i < size; i++)
    {
        //setting the rowVector to zero-one vector
        rowVector[i] = 1L;
        if (i > 0) rowVector[i-1] = 0L;
        //pushing rowVector into identityMatrixData
        identityMatrixData.push_back(rowVector);
    }
    return SquareMatrix(identityMatrixData);
}

您认为您可以做些什么?

我想我有两个选择:

  1. 将对象扔到堆上,然后尝试使用函数对其进行设置(这看起来毫无用处,因为您正在通过将其扔到堆上来重新定义刚刚定义的对象)
  2. 获得 c++11(或类似的东西)
  3. 使该函数成为返回 std::vector<SquareMatrix*> 的辅助函数大小为二(包含指向两个所需的 SquareMatrix 值的指针),并创建一个调用辅助函数并设置 Lptr 的函数, Uptr到返回的各个元素 vector .

我的选择就这么有限吗?

最佳答案

*Uptr = SquareMatrix(UData); LUDecompose() 是问题所在。 您不能将指针设置为函数返回时正在销毁的对象。然后指针是一个悬挂指针,无论何时您尝试使用它,它都会发生段错误。

您需要执行 Uptr = new SquareMatrix(UData);。然后在您的析构函数中,调用 delete Uptr;

如果您可以访问 C++11,则可以使用 std::unique_ptr 或任何指针容器/包装器。

您的选择示例:

#include <memory>

class Matrix
{
    public:
        Matrix() {}
        virtual ~Matrix() {}
};

class SqMatrix : public Matrix  //using raw pointers. You must remember to delete your pointers.
{
    private:
        SqMatrix* UPtr = nullptr;

    public:
        SqMatrix() : Matrix() {}
        void InitPtrs() {delete UPtr; UPtr = new SqMatrix();}
        ~SqMatrix() {delete UPtr;}
};

class OMatrix : public Matrix //No need to worry about clean up.
{
    private:
        std::unique_ptr<OMatrix> OPtr;

    public:
        OMatrix() : Matrix() {}
        void InitPtrs() {OPtr.reset(new OMatrix());}
        ~OMatrix() {}
};

另一种选择是将其存储在 vector 中。

关于c++ - 取消引用指向其类内对象的指针,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20590187/

相关文章:

c - 仅在第一行写入 csv 文件时矩阵输出错误 [C]

指向结构的指针的 C typedef

c++ - 指针的 const 说明符

c++ - 套接字很少返回 -1 (errno 97)

c++ - MFC CString 格式给出了奇怪的行为

c++ - 删除函数内的对象

oop - TypeScript 使用实例访问静态变量

c++ - Python Pandas 与 C++ 文本 CSV 数据导入解决方案的性能对比

c++ - 隐式好友类库

oop - 哪个设计更好?