我正在尝试编写这段代码,其中有 2 个类继承自父类。但是当我尝试在链接列表中使用它时,这段代码会中断。这是代码
class airship{
public:
airship(){};
// virtual ~ airship();
virtual void setData (string)=0;
virtual void showData()=0;
void setNext(airship* newptr);
airship* getNext();
void setType(int n){airshipType=n;}
int getType(){return airshipType;}
void setCount(int n){maxPassengerCount=n;};
int getCount(){return maxPassengerCount;}
void setWeight(int n){maxCargoWeight=n;}
int getWeight(){return maxCargoWeight;}
protected:
int maxPassengerCount;
int maxCargoWeight;
int airshipType;
private:
airship* next;
};
class airplane: public airship{
protected:
const char* airplaneName;
int engineType;
double range;
public:
airplane():airship(){};
// ~ airplane();
void setData(string);
void showData();
void setEngine(int n){engineType=n;}
int getEngine(){return engineType;}
void setRange(int n){range=n;}
int getRange();
};
class balloon : public airship{
protected:
const char* balloonName;
int gasType;
double maxAltitude;
public:
balloon():airship(){};
// ~balloon();
void setData(string);
void showData();
void setGas(int);
int getGas();
void setAlt(int);
int getAlt();
};
class mylist{
private:
airship* headAirship;
public:
void createlist(fstream&);
void setAirship(airship* Node){Node=headAirship;}
airship* getAirship(){return headAirship;}
};
void airship::setNext(airship* Node)
{
this->next=Node;
}
void airplane::setData(string line)
{
string line1;
bool flag=true;
int npos=0,nend,count=0;
while(line.c_str()!=NULL && flag)
{
nend=line.find(",",npos);
if (nend!=-1)
{
line1=line.substr(npos,nend);
}
else
{
line1=line;
flag=false;
}
line=line.substr(nend+1,line.length());
if (count==0)
this->airshipType=atoi(line1.c_str());
else if(count==1)
this->airplaneName=line1.c_str();
else if(count==2)
this->maxPassengerCount=atoi(line1.c_str());
else if(count==3)
this->maxCargoWeight=atoi(line1.c_str());
else if(count==4)
this->engineType=atoi(line1.c_str());
else
this->range=atoi(line1.c_str());
count=count+1;
}
}
void balloon::setData(string line)
{
string line1;
bool flag=true;
int npos=0,nend,count=0;
while(line.c_str()!=NULL && flag)
{
nend=line.find(",",npos);
if (nend!=-1)
{
line1=line.substr(npos,nend);
}
else
{
line1=line;
flag=false;
}
line=line.substr(nend+1,line.length());
if (count==0)
this->airshipType=atoi(line1.c_str());
else if(count==1)
this->balloonName=line1.c_str();
else if(count==2)
this->maxPassengerCount=atoi(line1.c_str());
else if(count==3)
this->maxCargoWeight=atoi(line1.c_str());
else if(count==4)
this->gasType=atoi(line1.c_str());
else
this->maxAltitude=atoi(line1.c_str());
count=count+1;
}
}
void mylist::createlist(fstream &myfile)
{
string ipdata;
int type;
while (getline(myfile,ipdata))
{
airship* newNode;
type=ipdata.find(",",0);
string ch=ipdata.substr(type-1,type);
if (atoi(ch.c_str())==0)
{
airplane* planeNode=new airplane();
planeNode->setData(ipdata);
newNode=planeNode;
}
else
{
balloon* balloonNode=new balloon();
balloonNode->setData(ipdata);
newNode=balloonNode;
}
newNode->setNext(headAirship);
this->headAirship=newNode;
}
}
int main()
{
mylist* list=0;
airship* firstNode=0,*otherNode=0;
/*if(argv[1]==NULL)
{
cout<<"File not Found"<<endl;
return -1;
}*/
fstream ipFile("data.txt",ios::in | ios::out);
if(ipFile.is_open())
{
list->createlist(ipFile);
}
list->setAirship(firstNode);
return 0;
}
plane::setdata(string) 函数无法正常工作,并且无法在 createList 函数中检测到 headAirship 指针。这就是为什么当我尝试执行此操作时在 createlist 函数中出现内存异常错误:newNode- >setNext(headAirship);
Assignment4Solution2.exe 中 0x00912ae0 处的未处理异常:0xC0000005:
访问冲突读取位置 0x00000000。