我正在尝试编写此代码,其中有两个类从父类继承。但是当我尝试在链接列表中使用它时,此代码会中断。这是代码
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;
}
airplane :: setdata(string)函数无法正常工作,并且无法在createList函数中检测headAirship指针。这就是为什么当我尝试执行此操作时,我在createlist函数中出现内存异常错误的原因:newNode-&GT; setNext(headAirship);
Assignment4Solution2.exe中0x00912ae0处的未处理异常:0xC0000005:
访问冲突读取位置0x00000000。
答案 0 :(得分:2)
我认为问题在于:
mylist* list=0;
在该行中,您创建了一个指向 myList 对象的指针,但是您没有指向任何此类对象;它指向地址0
然后你做
list->createlist(ipFile);
由于list没有指向包含 myList 对象的地址,因此调用任何myList方法都会产生您观察到的错误:访问冲突读取位置0x00000000。
相反,当您创建列表指针时,调用myList的构造函数以便创建对象并初始化指针以指向它
myList * list = new myList();
希望有所帮助:)