类内部的Realloc指针也是一个不起作用的指针



我有以下问题。

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

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

它在visualstudio(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

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

我修改了文件

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

最新更新