我試圖執行下面的代碼,除了一件事情之外,一切都很好,而且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類)創建隊列(櫃員類),然後根據每個隊列的時間,它決定在哪個隊列把下一個客戶?
我從'getLeast'和'getMax'函數調用了'else',它們工作正常。而且我還創建了**構造函數**來正確初始化該值。事情現在好了。 –