【问题标题】:Realloc pointer inside a Class that is also a pointer not working类中的 Realloc 指针也是一个不起作用的指针
【发布时间】:2019-04-04 03:57:08
【问题描述】:

我有以下问题。

我有一个包含几个私有变量(常量和指针)的类。

我使用这个类构建了一个对象,这个对象是一个指针。它是为了保存几个表而构建的。 我的类 *object;

虽然几天前我开始在 Linux 上的 g++ 上构建它,但它在 Visual Studio (Windows) 上运行良好。由于某种原因无法编译。

私有变量中的指针是动态的,所以我不需要从一开始就告诉我的代码我的私有指针有多大,也不需要表的数量。 像这样

Table 1
*Var1    *Var2      *Var3     *Var4
Entry 1   ....      ....      ....
.
.
.
.
Entry n   ....      ....      ....

问题是,当编译器到达我的对象的 realloc 时,它运行良好,没有错误。但是当它到达我的私有指针的 realloc 时,它崩溃了。我想这对他们来说是一个不同的脚本(或修改过的),因为他们有正确的原型。

你能帮我解决这个问题吗?

谢谢

类.h

class rel_perm
{
public:
    rel_perm();
    ~rel_perm();
    //Consulting methods
    double  getKROWC();
    int getNPTL();
    int getNPTG();
    double* getSW();
    double* getKRW();
    double* getKROW();
    double* getPCOW();
    double* getSG();
    double* getKRG();
    double* getKROG();
    double* getPCOG();
    //Changing methods
    void chgKROWC   (double x);
    void chgNPTL    (int x);
    void chgNPTG    (int x);
    void chgSW  (double x,int i);
    void chgKRW (double x,int i);
    void chgKROW    (double x,int i);
    void chgPCOW    (double x,int i);
    void chgSG  (double x,int i);
    void chgKRG (double x,int i);
    void chgKROG    (double x,int i);
    void chgPCOG    (double x,int i);
    //replace methods
    void rePCOG(double *x);
    void rePCOW(double *x);
private:
    double KROWC;
    int    NPTL;
    int    NPTG;
    double *SW;
    double *KRW;
    double *KROW;
    double *PCOW;
    double *SG;
    double *KRG;
    double *KROG;
    double *PCOG;
};

class.cpp

#include "iostream"
#include "rel_perm.h"
#include "stdlib.h"
using namespace std;
rel_perm::rel_perm()
{
}

rel_perm::~rel_perm()
{
}


//Consulting methods
double rel_perm::getKROWC()     { return KROWC; }
int    rel_perm::getNPTL()      {return NPTL;}
int    rel_perm::getNPTG()      {return NPTG;}
//double* for returning a pointer
double* rel_perm::getSW()   {return SW;}
double* rel_perm::getKRW()  {return KRW;}
double* rel_perm::getKROW() {return KROW;}
double* rel_perm::getPCOW() {return PCOW;}
double* rel_perm::getSG()   {return SG;}
double* rel_perm::getKRG()  {return KRG;}
double* rel_perm::getKROG() {return KROG;}
double* rel_perm::getPCOG() {return PCOG;}

//Changing methods
void rel_perm::chgKROWC(double x){
    KROWC = x;
}
void rel_perm::chgNPTL  (int x){
    NPTL = x; }
void rel_perm::chgNPTG  (int x){
    NPTG = x; }
void rel_perm::chgSW    (double x, int i){
    SW = (double *) realloc (double SW, (i + 1) * sizeof(double));
    *(SW + i) = x;
}
void rel_perm::chgKRW   (double x, int i){
    KRW = (double *)realloc(KRW, (i + 1)*sizeof(double));
    *(KRW + i) = x;
}
void rel_perm::chgKROW  (double x, int i){
    KROW = (double *)realloc(KROW, (i + 1)*sizeof(double));
    *(KROW + i) = x;
}
void rel_perm::chgPCOW  (double x, int i){
    PCOW = (double *)realloc(PCOW, (i + 1)*sizeof(double));
    *(PCOW + i) = x;
}
void rel_perm::chgSG    (double x, int i){
    SG = (double *)realloc(SG, (i + 1)*sizeof(double));
    *(SG+ i) = x;
}
void rel_perm::chgKRG   (double x, int i){
    KRG = (double *)realloc(KRG, (i + 1)*sizeof(double));
    *(KRG + i) = x;
}
void rel_perm::chgKROG  (double x, int i){
    KROG = (double *)realloc(KROG, (i + 1)*sizeof(double));
    *(KROG + i) = x;
}
void rel_perm::chgPCOG  (double x, int i){
    PCOG = (double *)realloc(PCOG, (i + 1)*sizeof(double));
    *(PCOG + i) = x;
}
//replace methods
void rel_perm::rePCOG(double *x){
    PCOG = x;
}
void rel_perm::rePCOW(double *x){
    PCOW = x;
}

这是我使用我的类的代码。

#include "string"
#include "global.h"

using namespace std;
//keyw = Keywords read from data file


void read_kr(string keyw, ifstream& infile){
    double x;
    int y;

    if (keyw == "SRP"){
        NSRP++;
        infile >> y;
        rt_perm = (rel_perm *)realloc(rt_perm, (y)*sizeof(rel_perm));
        rel_perm temp; //Temporary empty object
        rt_perm[y - 1] = temp; //Empty object assigned to "y" position
        while (!infile.eof())
        {
            infile >> keyw;
            if (keyw == "SWO"){
                string val; //Temporary variable
                int nptl = 1;
                double KROWC;
                while (!infile.eof())
                {
                    infile >> val;

                    if (isdigit(val[0])){
                        x = stod(val);
                        break;
                    }
                }
                for (int i = 0; i < 40; i++)
                {
                    rt_perm[y - 1].chgSW(x, i);
                    infile >> x;
                    rt_perm[y - 1].chgKRW(x, i);
                    infile >> x;
                    if (i == 0){
                        KROWC = x;
                    }
                    rt_perm[y - 1].chgKROW(x, i);
                    infile >> x;
                    rt_perm[y - 1].chgPCOW(x, i);
                    infile >> val;
                    if (!isdigit(val[0])){
                        rt_perm[y - 1].chgNPTL(nptl);
                        rt_perm[y - 1].chgKROWC(KROWC);
                        break;
                    }
                    x = stod(val);
                    nptl++;
                }
            }
            if (keyw == "SGL"){
                string val; //Temporary variable
                int nptg = 1;
                while (!infile.eof())
                {
                    infile >> val;

                    if (isdigit(val[0])){
                        x = stod(val);
                        break;
                    }
                }
                for (int i = 0; i < 40; i++)
                {
                    rt_perm[y - 1].chgSG(x, i);
                    infile >> x;
                    rt_perm[y - 1].chgKRG(x, i);
                    infile >> x;
                    rt_perm[y - 1].chgKROG(x, i);
                    infile >> x;
                    rt_perm[y - 1].chgPCOG(x, i);
                    infile >> val;
                    if (!isdigit(val[0])){
                        rt_perm[y - 1].chgNPTG(nptg);
                        break;
                    }
                    if (infile.eof()){
                        rt_perm[y - 1].chgNPTG(nptg);
                        break;
                    }
                    x = stod(val);
                    nptg++;
                }
                break;
            }
        }
        temp.~rel_perm();
        //Wonderful
    }
}

这是在 main.cpp 中作为一个全局构建的。

rel_perm *rt_perm;  //Object contains "N" tables

【问题讨论】:

    标签: class object pointers dynamic


    【解决方案1】:

    这是我自己的答案,我想通了。我没有给这些指针初始值。以下是我找到的解决方案。

    我修改了文件

    class.cpp

    #include "iostream"
    #include "rel_perm.h"
    #include "stdlib.h"
    using namespace std;
    rel_perm::rel_perm()
    {
        SW=NULL;
        KRW=NULL;
        KROW=NULL;
        PCOW=NULL;
        SG=NULL;
        KRG=NULL;
        KROG=NULL;
        PCOG=NULL;
    }
    
    rel_perm::~rel_perm()
    {
    }
    
    
    //Consulting methods
    double rel_perm::getKROWC()     { return KROWC; }
    int    rel_perm::getNPTL()      {return NPTL;}
    int    rel_perm::getNPTG()      {return NPTG;}
    //double* for returning a pointer
    double* rel_perm::getSW()   {return SW;}
    double* rel_perm::getKRW()  {return KRW;}
    double* rel_perm::getKROW() {return KROW;}
    double* rel_perm::getPCOW() {return PCOW;}
    double* rel_perm::getSG()   {return SG;}
    double* rel_perm::getKRG()  {return KRG;}
    double* rel_perm::getKROG() {return KROG;}
    double* rel_perm::getPCOG() {return PCOG;}
    
    //Changing methods
    void rel_perm::chgKROWC(double x){
        KROWC = x;
    }
    void rel_perm::chgNPTL  (int x){
        NPTL = x; }
    void rel_perm::chgNPTG  (int x){
        NPTG = x; }
    void rel_perm::chgSW    (double x, int i){
        SW = (double *) realloc (SW, (i + 1) * sizeof(double));
        *(SW + i) = x;
    }
    void rel_perm::chgKRW   (double x, int i){
        KRW = (double *)realloc(KRW, (i + 1)*sizeof(double));
        *(KRW + i) = x;
    }
    void rel_perm::chgKROW  (double x, int i){
        KROW = (double *)realloc(KROW, (i + 1)*sizeof(double));
        *(KROW + i) = x;
    }
    void rel_perm::chgPCOW  (double x, int i){
        PCOW = (double *)realloc(PCOW, (i + 1)*sizeof(double));
        *(PCOW + i) = x;
    }
    void rel_perm::chgSG    (double x, int i){
        SG = (double *)realloc(SG, (i + 1)*sizeof(double));
        *(SG+ i) = x;
    }
    void rel_perm::chgKRG   (double x, int i){
        KRG = (double *)realloc(KRG, (i + 1)*sizeof(double));
        *(KRG + i) = x;
    }
    void rel_perm::chgKROG  (double x, int i){
        KROG = (double *)realloc(KROG, (i + 1)*sizeof(double));
        *(KROG + i) = x;
    }
    void rel_perm::chgPCOG  (double x, int i){
        PCOG = (double *)realloc(PCOG, (i + 1)*sizeof(double));
        *(PCOG + i) = x;
    }
    //replace methods
    void rel_perm::rePCOG(double *x){
        PCOG = x;
    }
    void rel_perm::rePCOW(double *x){
        PCOW = x;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-01-28
      • 2020-06-30
      • 2023-03-07
      • 2016-10-10
      相关资源
      最近更新 更多