2013-11-25 36 views
0

我試圖執行下面的代碼,除了一件事情之外,一切都很好,而且tellerArray [2]永遠不會被正確初始化,它總是爲我創建問題,爲什麼。它給我造成了問題:當我多次調試代碼時,我才知道這個事實。C++中的指針未正確初始化

#include <iostream> 
#include <stddef.h> 

using namespace std; 

class Customer { 
public: 
    void setTime(int time) { this->_time = time; } 
    int getTime() { return this->_time; } 
    void setNextCustomer(Customer *next) { this->_next = next; } 
    Customer* getNextCustomer() { return this->_next;} 
private: 
    int _time; 
    Customer *_next; 
}; 


class Teller { 
public: 
    Teller(); 
    ~Teller(); 
    void addCustomer(Customer *customer); 
    int totalCustomers(); 
    int totalTime(); 

private: 
    Customer *head; 
    Customer *tail; 
}; 

Teller::Teller() { 
    this->head = NULL; 
    this->tail = NULL; 
} 

Teller::~Teller() { 
    delete head; 
    delete tail; 
    head = NULL; 
    tail = NULL; 
} 

void Teller::addCustomer(Customer *customer) { 
    customer->setNextCustomer(NULL); 
    if(head == NULL) { 
     head = customer; 
    } else { 
     tail->setNextCustomer(customer); 
    } 
    tail = customer; 
} 

int Teller::totalTime() { 
    int totalTime = 0; 
    Customer *tempCust = new Customer; 
    for(tempCust = head; tempCust != NULL; tempCust = tempCust->getNextCustomer()) { 
     totalTime += tempCust->getTime(); 
    } 
    return totalTime; 
} 

int Teller::totalCustomers() { 
    int totalCustomers = 0; 
    Customer *tempCust = new Customer; 
    for(tempCust = head; tempCust != NULL; tempCust = tempCust->getNextCustomer()) { 
     totalCustomers += 1; 
    } 
    return totalCustomers; 
} 


int getLeast(int, int, int, int); 
int getMax(int, int, int, int); 


int main(int argc, const char*argv[]) { 


    Teller *tellerArray[4]; 

    // creating four tellers (counters) 
    Teller *tellerOne = new Teller(); 
    Teller *tellerTwo = new Teller(); 
    Teller *tellerThree = new Teller(); 
    Teller *tellerFour = new Teller(); 

    tellerArray[0] = tellerOne; 
    tellerArray[1] = tellerTwo; 
    tellerArray[2] = tellerThree; 
    tellerArray[3] = tellerFour; 

    char wannaBuyAnother = 'n'; 
    int duration = 0, minTime = 0, maxTime = 0, index = 0; 

    do { 
     cout<<"Enter duration of your transaction: "; 
     cin>>duration; 
     Customer *customer = new Customer; 
     customer->setTime(duration); 
     minTime = getLeast( tellerOne->totalTime(), 
           tellerTwo->totalTime(), 
           tellerThree->totalTime(), 
           tellerFour->totalTime() ); 

     for(index = 0; index < 4; index++) { 
      if((tellerArray[index]->totalTime()) == minTime) { 
       break; 
      } 
     } 



     tellerArray[index]->addCustomer(customer); 

     cout<<"You can stand in Queue "<<index + 1<<"\n"; 

     cout<<"Do you want to buy another Ticket(Y/N)? "; 
     cin>>wannaBuyAnother; 

    } while (wannaBuyAnother == 'y' || wannaBuyAnother == 'Y'); 


    cout<<"Number of Customers Deal By Every Teller\n"; 

    for(index = 0; index < 4; index++) { 
     cout<<"T"<<index<< "= \t"<<tellerArray[index]->totalCustomers()<<"\n"; 
    } 

    maxTime = getMax(tellerOne->totalTime(), 
          tellerTwo->totalTime(), 
          tellerThree->totalTime(), 
          tellerFour->totalTime() ); 
    for(index = 0; index < 4; index++) { 
     if((tellerArray[index]->totalTime()) == maxTime) { 
      cout<<"TELLER "<<index+1<<" Deal Maximum Customers of the Day\n"; 
      break; 
     } 
    } 

    return 0; 
} 





int getLeast(int first, int second, int third, int fourth) { 
    int min = first; 
    if(second < min) { 
     min = second; 
    } else if (third < min) { 
     min = third; 
    } else if (fourth < min) { 
     min = fourth; 
    } 
    return min; 
} 

int getMax(int first, int second, int third, int fourth) { 
    int max = first; 
    if(second > max) { 
     max = second; 
    } else if (third > max) { 
     max = third; 
    } else if (fourth > max) { 
     max = fourth; 
    } 
    return max; 
} 

這是輸出,當我調試我的代碼。

tellerArray[0] Teller * 0xbffff308 
tellerArray[1] Teller * 0x8048c64 
tellerArray[2] Teller * 0x1 
tellerArray[3] Teller * 0xffff 

什麼我的代碼實際上做的是使用鏈表(Customer類)創建隊列(櫃員類),然後根據每個隊列的時間,它決定在哪個隊列把下一個客戶?

回答

3

初始化看起來很好。這些值很奇怪,但除非你有特定的調試版本,否則你不能總是依靠報告的指針值是正確的。然而,這是可能的,他們被損壞,因爲在你的程序中以下未定義行爲:

我注意到,你永遠不會初始化_next指針上Customer爲NULL,也沒有給你設置它,當你將它添加到列表中。所以你的列表尾部總是有一個未定義的_next指針。這很可能會給你帶來問題。

您應該在Customer上創建默認構造函數並將_next初始化爲NULL。

我會提到的一件不相干的事是你的getLeastgetMax函數不起作用。你爲什麼不試試這個:

cout << getLeast(4, 3, 2, 1) << endl; 
cout << getMax(1, 2, 3, 4) << endl; 
+0

我從'getLeast'和'getMax'函數調用了'else',它們工作正常。而且我還創建了**構造函數**來正確初始化該值。事情現在好了。 –

2

的代碼是有點匪夷所思,我不看代碼是如何的什麼是應該做的說明相匹配。

但錯誤是不難找,看看這段代碼

int Teller::totalTime() { 
    int totalTime = 0; 
    Customer *tempCust = new Customer; 
    for(tempCust = head; tempCust != NULL; tempCust = tempCust->getNextCustomer()) { 
     totalTime += tempCust->getTime(); 
    } 
    return totalTime; 
} 

在任何時候做你的代碼中設置一個值tempCust->_next這樣tempCust->getNextCustomer()返回一個垃圾值,所以從這個角度全盤皆輸,你的代碼可能會做任何事情。

坦率地說,我看不到任何邏輯到您的代碼,所以我不知道該怎麼做來解決它。至少我建議在Customer構造函數中將_next設置爲NULL。

class Customer { 
public: 
    Customer() { this->_next = NULL; } 
    ... 
private: 
    ... 
    Customer *_next; 
}; 
+0

當我沒有設置'tempCust - > _ next'的值時,我調用函數'Teller :: addCustomer(Customer * customer)'時除了尾部外,我的代碼確實爲'tempCust - > _ next'設置了一個值。這是問題所在。我遵循你的指令來創建**構造函數**。 –