将指针重用于循环中的类

时间:2014-04-04 19:06:36

标签: c++ pointers

在程序中有一个指向类对象WordList *TheList;的指针。 WordList具有WordDataList和WordDataDLinkList的子类,因此在case语句中,我们解释了要使用的子类以及如何打印列表中的信息。根据我在规范中的理解,每个案例都应该将TheList声明为该类型的指针并使用它,然后在案例结尾处回收内存,以便它可以在循环的下一次迭代中使用。当我尝试类似的东西时:

    while (true)
{
    displayMenu();
    cin>>selection;
    switch(selection)
    {
    case '1':
        TheList = new WordDataList;
        TheList->parseIntoList(inf);
        TheList->printIteratively();
        delete TheList;
        break;
    case '2':
        TheList = new WordDataList;
        TheList->parseIntoList(inf);
        TheList->printRecursively();
        delete TheList;
        break;
    case '3':
        TheList = new WordDataList;
        TheList->parseIntoList(inf);
        TheList->printPtrRecursively();
        delete TheList;
        break;
    case '6':
        cout<<"Goodbye"<<endl;
        return 0;
    default:
        cout<<"I cannot understand "<<selection<<".  Try again."<<endl;
        break;
    } // switch
} // while

删除指针使得在第一次运行后没有数据出现(菜单仍然出现),选项2结束了seg faulting。我正在修改我教授提供的代码,当他没有删除调用,并且在循环之前新的WordDataList和parseIntoList运行正常。有什么建议吗?

添加了:

我在每种情况下重新初始化TheList,因为我将添加将使用WordDataDLinkList的4和5。如果它是case语句外部的指向WordDataList的指针,那么在需要时如何将其更改为WordDataDLinkList?我的教授为我们写了WordDataList:

   #include <sstream>
   #include <iostream>
   #include "WordDataList.h"

   using namespace std;

     WordDataList::WordDataList()
     { numWords=0; }

     bool WordDataList::incMatch(string temp)
     { for(int i=0; i<numWords; i++) {
         if (temp==TheWords[i].getWord()) {
           TheWords[i].incCount();
           return true;
         }
       }  
       return false;
     }

     void WordDataList::parseIntoList(ifstream &inf)
     { string temp;
       while (inf >> temp) 
         if (!incMatch(temp) && numWords < 10) {
             TheWords[numWords].setWord(temp);
             TheWords[numWords++].setCount(1);
         }
     }

     // Print the data iteratively
     void WordDataList::printIteratively()
   //  void printObjectArrayIterator(WordData TheWords[], int numWords)
   {
     cout<<"--------------------------"<<endl;
     cout<<"|Object  Array  Iterative|"<<endl;
     cout<<"|Word         Occurences |"<<endl;  
     cout<<"--------------------------"<<endl;
     for(int i=0; i<numWords; i++)
       cout<<" "<<TheWords[i]<<endl;
   }


     // Print the data recursively
     void WordDataList::printRecursivelyWorker(int numWords)
     //void printObjectArrayRecursive(WordData TheWords[], int numWords)
     {if (numWords==1) {
         cout<<"--------------------------"<<endl;
         cout<<"|Object  Array  Recursive|"<<endl;
         cout<<"|Word         Occurences |"<<endl;  
         cout<<"--------------------------"<<endl;
         cout<<" "<<TheWords[numWords-1]<<endl;
         return;
       }
       printRecursivelyWorker(numWords-1);
       cout<<" "<<TheWords[numWords-1]<<endl;
     }

     // Call worker function to print the data recursively
     void WordDataList::printRecursively()
     {  printRecursivelyWorker(numWords); }


     // Print the data recursively with a pointer
     void WordDataList::printPtrRecursivelyWorker(int numWords)
     //void printObjectArrayPointerRecursive(WordData* TheWords, int numWords)
     {if (!numWords)
       { cout<<"--------------------------"<<endl;
         cout<<"|Object  Array  Pointer  |"<<endl;
         cout<<"|Word         Occurences |"<<endl;  
         cout<<"--------------------------"<<endl;
         return;
       }
       printPtrRecursivelyWorker(numWords-1);
       cout<<" "<<*(TheWords+(numWords-1))<<endl;
     }

     // Call worker function to print the data recursively
     void WordDataList::printPtrRecursively()
     { printPtrRecursivelyWorker(numWords); }

2 个答案:

答案 0 :(得分:0)

我觉得你对其他事感到困惑。为什么你需要继续重新创建&#39; WordDataList&#39;是修改它的解析和打印方法吗?

如果没有,只需创建一次,然后只需使用select来选择要使用的打印功能。

我还建议将每个案例执行语句集放入一个闭包中,并添加一些打印输出或单步执行调试器以查看发生了什么。主要猜测是你的新的&#39;返回NULL,这样你就无法正确调用它的成员,或者析构函数是坏的。

答案 1 :(得分:0)

好的,我可以告诉你:选项1-3应该为你的指针TheList选择一个不同的子类。 TheList是指向基类的指针,因此很好。我认为你需要做的是new每个switch语句中的相应子类。即。

case '1': TheList = new WordDataDLinkList();

编辑:如果您打算根据当前使用的classtype调用每个成员函数的不同版本,如WordDataDLinkList::parseIntoList(inf),而不是WordDataList::parseIntoList(inf),请尝试阅读{{3} }