在一个类中重新分配指针,该类也是指针不起作用

时间:2018-10-31 03:01:05

标签: class object pointers dynamic

我有以下问题。

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

我使用此类创建了一个对象,该对象是一个指针。它被创建为保存多个表。 Myclass *对象;

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

私有变量中的指针是动态的,因此我不需要从一开始就告诉我的代码,私有指针或表的数量将是多少。 像这样

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

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

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

谢谢

  

class.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

1 个答案:

答案 0 :(得分:0)

这是我自己的答案,我知道了。我没有为这些指针提供初始值。以下是我找到的解决方案。

我修改了文件

  

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;
}