2013-04-11 29 views
2

我有一个指针和范围的问题。我正在尝试维护一个指向对象的链接列表数组。当我尝试push_front()在一个功能,它的作品。但是,如果我尝试在我的程序的另一部分遍历列表,则列表不再包含任何数据,而我的指针也不好。C++链接列表功能之间丢失数据

这是我的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;    

的addPerson的功能,其中的printList正常工作:

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的调用时,其工作方式的printList功能,但不是在parseCommands:

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)持有散列表。

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 
}; 
+0

你可能会考虑为你的散列表模板定义一个节点类型,该节点类型保存了被推入的项目的by-val副本(这可能是一个指针,如果是的话,一个*智能指针*允许多态访问),碰撞列表指针数据作为* node *的一部分进行管理,而不是部分被添加的项目的用户类型需求。除此之外,下面的答案解决了眼前的问题。 – WhozCraig 2013-04-11 21:55:25

回答

1

问题出在这两个地方。

universityList.addPerson(adminPtr); 

//... 

您正在传递adminPtr的副本。

template <typename T> 
void HashTable<T>::addPerson(T newPerson) { //newPerson is a pointer to a person object 
// ... 
    table[loc].push_front(&newPerson); // add to list at table[loc] passing address of pointer to person 
// .... 
    } 

newPerson是一个局部变量来addPerson。它返回时不再有效。但是你将它的地址添加到表格中。

的问题是,

list <T*> table[tableSize]; 

被存储指向人的指针。

我不认为通过引用也能解决问题。因为那你将依赖于这里自动创建的指针。

Administrator *adminPtr = new Administrator(); 

什么adminPtr指针指向将保持但不adminPtr本身。所以你不能依赖它的地址(,除非你是在创建它的同一个函数中静默)。解决这个问题的一种可能的方法是动态分配adminPtr本身。

Administrator **adminPtr = new Administrator*; 
adminPtr = new Administrator(); 

但也许你应该修改要求。

+0

@ user2048220按值传递并不仅限于指针。任何事情都可以通过价值传递。这意味着什么将会被复制。问题是,当一个**自动创建的变量**(指针与否)的地址。 **一旦您从创建它的函数返回,地址就无效了。 – 2013-04-12 20:56:56

1

您的表声明如下:

list <T*> table[tableSize]; 

这意味着它包含了需要进行动态分配,还是需要保持在范围内的容器的整个生命周期的任何指针。不是这种情况。

table[loc].push_front(&newPerson); 

你应该做下列之一:在您的功能addPerson你一个局部变量的地址添加

  1. 更改表list<T>对象的数组。
  2. 动态复制数据。 table[loc].push_front(new T(newPerson))

因为这是一个名单,我会去的选项1,因为该名单将在本地复制无论如何,你不会有后来清理指针。第三个选项是使用list<unique_ptr<T> >或类似的。

+0

谢谢!选项1是我所需要的! – jenwoodson 2013-04-12 20:51:49