1

私はこのコードを書いて、自分自身でファイルや文字列で正しく動作する方法を学びます。私はファイルへの読み書きの部分でうまく管理しましたが、リストからすべての要素を削除する時点でしばらく積み重ねました。リンクされたリストから正しい要素を削除するには?

問題は、私が関数printAllを使用しようとしているリストからすべての要素を削除した後、それが起こるはずがなく、問題がどこにあるのか分からないということです。あなたの中には、バグについて私の目を開いて修正するのに役立つ人もいます。

ので、このコードは、変数が文字列の文字列やベクトルでUnitという名前の単一リンクリストクラスの実装です:ここ

class Unit { 
public: 
Unit();//constructor 
void printAllUnits(Unit *head);//-v 
void printUnit(Unit *unit);//-v 
Unit *insert(Unit *head,Unit *element);//-v 
Unit *getNextUnit(Unit **unit);//-v 

Unit *MergeBySN(Unit *leftPtr ,Unit *rightPtr);//-v 
void SplitListBySn(Unit* head,Unit** left, Unit** right);//-v 
int menuSelection(int correct);//prints a menu-v 
void findAndPrintUnitByType(Unit *head, string searchString); 
void findAndPrintUnitBySN(Unit *head, string number); 
void findAndPrintUnitBySNType(Unit *head,string type, string number); 
void findAndPrintUnitByCell(Unit *head, string cell); 
void findAndPrintUnitByUnitLocation(Unit *head, string location); 
void findAndPrintUnitByDate(Unit *head, string date); 
Unit *readFromFile(Unit *head); 
void writeToNewFile(Unit *head); 
void addToFile(Unit *head); 
int checkNumber(string *numberStr); 

Unit *deleteUnit(Unit *unit, string type, string sn);//-v 
void deleteAll(Unit *unit); 

int getChoice(int lower,int upper); 
void SortListBySn(Unit **unit);//-v 
Unit *SortListByType(Unit *unit);//-v 
void mainmenu();//-v 
Unit *subInsert(Unit *head,Unit *element); 
~Unit(); //destructor 

**/*variables*/** 
string Type;//type of unit 
string SN;//serial number of the unit 
int occur;//number of occurences of this serial number during service 
vector<string> Cell;//phisical location 
vector<string> Date;//vactor to hold the dates of changes 
vector<string> unitLocation; 
Unit *next;//link to next unit 
}; 

は機能DELETEALLとのprintAllとデストラクタとコンストラクタのいくつかの実装である:

/*constructor*/ 
Unit::Unit(){ 
    Type.clear(); 
    SN.clear(); 
    occur=1; 
    Cell.clear(); 
    unitLocation.clear(); 
    next=NULL; 
    Date.clear(); 
} 
/*function to print all elements in the list*/ 
void Unit::printAllUnits(Unit *head){ 
    Unit *tmp; 
    tmp = head; 
    cout<<endl; 
    if (tmp == NULL) 
    { 
    cout<<endl<<"There is no units, Nothing to Print.\n"<<endl; 
    } 
    else 
    { 
    cout<<"  Type  SN  Cell UnitLocations Date "<<endl; 
    cout<<"  ------ ------ ------ -------------- ------ "<<endl; 
    while (tmp != NULL) 
    { 
     printUnit(tmp); 
     tmp = tmp->next; 
    } 
    cout<<endl;cout<<endl; 
    } 
    } 
    /*function to print specific unit*/ 
void Unit::printUnit(Unit *unit){ 
    int i=1; 
    string type; 
    string cell; 
    string sn; 
    string location; 
    string date; 
    if(unit!=NULL){ 
    type=unit->Type; 
    sn=unit->SN; 
    cell=unit->Cell[0]; 
    location=unit->unitLocation[0]; 
    date=unit->Date[0]; 
    cout<<"\t"<<type<<"\t"<<sn<<"\t"<<cell<<"\t\t"<<location<<"\t"<<date<<endl; 
    for(;i < unit->Cell.size();i++){ 
      cell=unit->Cell[i]; 
      location=unit->unitLocation[i]; 
      date=unit->Date[i]; 
      cout<<"\t\t\t"<<cell<<"\t\t"<<location<<"\t"<<date<<endl; 
    } 
     cout<<"Occurency:\t"<<unit->occur<<endl; 
    } 
} 
/*destructor*/ 
Unit::~Unit(){ 
    this->Cell.~vector(); 
    this->Date.~vector(); 
    this->unitLocation.~vector(); 
    this->Type.~basic_string(); 
    this->SN.~basic_string(); 

    } 
/*function to insert element in to list by making a decision after what SN to insert it(kind of  insertion sort)*/ 

Unit *Unit::insert(Unit *head,Unit *element){ 
    Unit *currElement; 
    //*if empty list 
    if(head==NULL){ 
      return element; 
    }//if 
//*if empty 
    if(element->SN < head->SN){ 
      element->next=head; 
      return element; 
    }//if 

    currElement=head; 
    //*compare the serial numbers of elements 

    //*if more 
for(;currElement->next != NULL;currElement=currElement->next){ 
      if(element->SN < currElement->next->SN) 
     break; 
} 

//*equal numbers 
if(currElement->SN==element->SN){//if SN is equal 
    if(currElement->Type.compare(element->Type)==0){//if types are the same 
     currElement->Date.push_back(element->Date[0]); 
     currElement->occur++; 
     currElement->Cell.push_back(element->Cell[0]); 
     currElement->unitLocation.push_back(element->unitLocation[0]); 
    } 
return head; 
} 


//*put new element between current element and it's next elment 
//(if currelement is last then curentelemnt's next ==NULL) 
element->next=currElement->next; 
currElement->next=element; 
return head; 
    } 
    /*main prog*/ 
int main(int argc, char *argv[]){ 
bool flag=0; 
string str; 
string *type;//type of unit (
string *sern;//serial number of the unit 
string *cell;//phisical location 
string *date;//vactor to hold the dates 
string *unitlocation; 
Unit *head1=NULL; 


Unit *tmp=new Unit; 
    /*getting data*/ 
cin>>tmp->Type; 
cin>>tmp->SN; 
cin>>str;tmp->Cell.push_back(str); 
cin>>str;tmp->Date.push_back(str); 
cin>>str;tmp->unitLocation.push_back(str); 
/*inserting in to the list*/ 
    head1=head1->insert(head1,tmp); 
head1->printAllUnits(head1); 
head1->deleteAll(head1); 
head1->printAllUnits(head1); 
return 0; 
} 

問題は、私は、デバッガで見たように、DELETEALLアクションを行った後に発生する(VS2008)タイプとSNのリストを印刷しようとすると(それが空だが)<badptr>をしているとフィールド私がNULLか空リストのチェックをしたが、プログラムはそれを試み、クラッシュする。

したがって、何が問題なのですか?deleteALL?どのように実装すればよいですか?

+1

私はあなたが明示的にデストラクタを呼び出しているのを見ます。デストラクタも自動的に呼び出されるので、これだけでは間違いです。 – UncleBens

+0

関数deleteAllはどこですか? – Blazes

+0

あなたの "deleteAll"の実装を教えてください。コード内にはありません。 –

答えて

1

エラーは単純です。削除した後にhead1を印刷しようとしています。それは動作しません - あなたは削除されたオブジェクトのメソッドを呼び出すべきではありません。あなたは、コンストラクタのものclear呼び出しのいずれかを必要としない

  • だけで簡単に見た後、あなたのコードで多くの問題があります。いずれの場合も、ベクトルは初期化時に空です。

  • デストラクタを明示的に呼び出すべきではありません。彼らは自動的に呼び出されます。
  • headの一時コピーをprintAllUnitsに作成する必要はありません。
  • 文章には「単位はありません。印刷するものはありません」というエラーが2つあります。

deleteAllを実装する方法はいくつかあります。ここには単純なものがあります:

Unit * Unit::deleteAll(Unit * head) 
{ 
    vector<Unit*> units; 
    while (head) 
    { 
     units.push_back(head); 
     head = head->next; 
    } 
    for (int i(0); i != units.size(); ++i) 
     delete units.at(i); 
    return NULL; 
} 

int main() 
{ 
    // ... 
    head1 = head1->deleteAll(head1); 
    Unit::printAllUnits(head1); 
} 
関連する問題