2

ポインターとスコープに問題があります。オブジェクトへのポインターのリンクされたリストの配列を維持しようとしています。1 つの関数で push_front() を実行しようとすると、機能します。ただし、プログラムの別の部分でリストを反復処理しようとすると、リストにデータが含まれなくなり、ポインタが正しくありません。

これは私の parseCommands 関数の一部です。問題は、printList が呼び出されたときです。

                    Administrator *adminPtr = new Administrator(); // create new Administrator pointer

                    //local variables...
                    string adminName;   //administrator's name
                    int adminMNum;  //administrator's M Number
                    string adminEmail;  //administrator's email address
                    string adminTitle;  // administrator's title

                    // read in & store data for new administrator
                    inData >> adminName; //read in data
                    adminPtr->setName(adminName); //set admin name

                    inData >> adminMNum;
                    adminPtr->setMNum(adminMNum);   // set admin M Number

                    inData >> adminEmail;
                    adminPtr->setEmail(adminEmail); // set admin email address

                    inData >> adminTitle;
                    adminPtr->setTitle(adminTitle); //set admin title
                    // finished storing new administrator info

                    // add Administrator to list
                    cout << "Adding Administrator: " << endl;
                    cout << "in records office adminPtr/newPerson: " << adminPtr << endl;
                    universityList.addPerson(adminPtr);  // call addPerson--hashTable
                    //universityList.printPerson(adminPtr);     // print admin info using polymorphic method
                    //cout << "The load factor (alpha) is: " << universityList.getLength()/universityList.getMaxTableSize() << endl;    // print alpha
                    universityList.printList(adminPtr->getMNum());  // print all items at table[loc]--breaks here
                    cout << endl;               

printList が正常に機能する addPerson 関数:

template <typename T>
void HashTable<T>::addPerson(T newPerson) { //newPerson is a pointer to a person object
  int loc;  // array location provided by hashFunction
  cout << "in hashtable newPerson: " << newPerson << endl;
  loc = hashFunction(newPerson->getMNum());  // get loc
  table[loc].push_front(&newPerson);  // add to list at table[loc] passing address of pointer to person
  printList(newPerson->getMNum());  // print all items at table[loc]--works here
  size++;   // increment size
  } //end function

addPerson で呼び出されたときに機能するが、parseCommands では機能しない printList 関数:

template <typename T>
void HashTable<T>::printList(searchKeyType key) {   //print list of items held in array location
    int loc = hashFunction(key);    // get array location
    if (table[loc].empty()) {   // if list is empty
        cout << "Can not print person M" << key << " NOT found" << endl << endl; 
    }   //end if empty
  else{
    list<T*>::iterator iter;    // stl iterator
    iter = table[loc].begin();
    cout << "in printList table[loc]begin " << *iter << endl;   //print address of table[loc]begin.()--where iter points
    cout << "in printList item in table[loc]begin " << **iter << endl;  // print address of the pointer that iter points to
    while(iter != table[loc].end()) { // for each item in the list

        (**iter)->print();  // print person info using polymorphic method
        ++iter;
    }   //end for
  } // end else
}   // end printList

印刷機能:

void Administrator::print()const {  
// print Administrator info
cout << "   " << "Full Name:    " << getName() << endl;
cout << "   " << "M Number :    "<< getMNum() << endl;
cout << "   " << "Email Addr:   " << getEmail() << endl;
cout << "   " << "Title:        "   << getTitle() << endl;
}; // end print function

hashTable クラス:

template<typename T>
class HashTable{
public:
    HashTable(); // constructor
    bool isEmpty()const; //determines if the hash table is empty
    int getLength() const;  // returns (size) number of Persons in table (accessor)
    int getMaxTableSize() const;    // returns tableSize (size of array)
    void addPerson(T person);   // adds new Person 
    void removePerson(searchKeyType key);   // deletes Person from the HashTable
    void printPerson(T person); // prints Person info
    T getNodeItem(int mNumber); //returns person object (accessor)
    void printList(searchKeyType key);  //print list of items held in array location

private:
    int size;   // number of Persons in table
    static const int tableSize = 1; // number of buckets/array size -- planning on using 70001--assuming 35,000 entries at once; largest prime > 2*35000
    list <T*> table[tableSize]; // array of STL lists for chains
    int hashFunction(searchKeyType searchKey);  // hash function to return location (array index) of item
}; //end HashTable class

adminPtr を addPerson に渡すと、リストに追加されるようです。parseCommands 関数に戻ったときにデータが失われるのはなぜですか? スタックとヒープの問題ですか? どこかに「新しい」が必要ですか?何が起こっているのかを把握しようとしてポインターのアドレスを出力していたところに、余分な行がいくつかあります。

これは、私が解決できなかったクラスのプログラミングの問題でした。STL リンク リストの配列を使用して、ハッシュ テーブルをシミュレートする必要がありました。ベクター、マップなどの使用は許可されていませんでした。このプログラムには、派生クラス (管理者など) とテンプレート化されたハッシュ テーブル クラスを持つ抽象基本クラス (Person) が含まれています。ハッシュ テーブルを保持するクラス (RecordsOffice) がもう 1 つあります。

class RecordsOffice {
public:
    RecordsOffice();    // default constructor
    void parseCommands(string fileName);    // function to parse commands from a file to maintain the StudentList

private:
    HashTable <Person*> universityList; // creates empty hashtable
};
4

2 に答える 2