2012-10-30 1 views
0

타임 스탬프, 현재 층 및 대상 층별로 요청을 읽는 기능이 있으며 예상 한대로 출력되지 않습니다.Bool : 0 또는 1이 아닌 cout의 잘못된 출력

bool을 제외하고 타임 스탬프, 현재 층 및 대상 층 모두 내 구성원 값이 올바르게 출력됩니다.

부울은 내 방향으로 대신 1 또는 0을 출력합니다.

노드 번호 : 타임 스탬프

Elevator::readRequests() 

{ 
    ifstream myStream("T1.txt"); 

while(!myStream.eof()) 
{ 
    int timestamp ,currentFloor, destinationFloor; 


    myStream >> timestamp >> currentFloor >> destinationFloor; 
    //cout<< endl <<"The current timestamp is "<< timestamp << "The current floor is " << currentFloor 
    // << " and the destination floor is " << destinationFloor << endl << endl; 
    //cout<< endl; 

    reqNode *temp = new reqNode; 

    //initialize request node object 
    temp->timestamp = timestamp; 
    temp->start = currentFloor; 
    temp->destination = destinationFloor; 
    temp->start_time = -1; 
    temp->finish_time = -1; 

    temp->calculate_priority(); 

    if(temp->start < temp->destination) 
     temp->set_dir(true); 
    else 
     temp->set_dir(false); 

    request.push(*temp);//push nodes into the request bank 
} 
int i = 0; 
while(!request.empty()) 
{ 

    cout << "Node " << i << " : " << request.front().timestamp << " " << request.front().start << " " << request.front().destination 
     << " " << request.front().direction << endl; 

    request.pop();//popping the request in order to test 
    i++; 
} 


} 

나는 출력을 얻기 위해 노력하고 있어요. 현재 (사용자 층). 대상 (사용자 층). 방향 (사용자가 진행중)
Node 0 : 1 3 7 1 
Node 1 : 1 2 9 1 
Node 2 : 1 7 9 1 
Node 3 : 2 4 6 1 
Node 4 : 2 4 8 1 
Node 5 : 2 1 17 1 
Node 6 : 5 1 15 1 
Node 7 : 5 5 1 0 
Node 8 : 6 17 4 0 
Node 9 : 6 4 17 1 

대신 내가 출력으로 점점 오전 :

Node 0 : 1 3 7 205 
Node 1 : 1 2 9 205 
Node 2 : 1 7 9 205 
Node 3 : 2 4 6 205 
Node 4 : 2 4 8 205 
Node 5 : 2 1 17 205 
Node 6 : 5 1 15 205 
Node 7 : 5 5 1 205 
Node 8 : 6 17 4 205 
Node 9 : 6 4 17 205 

이 파일 T1.txt입니다 :

1 3 7 
1 2 9 
1 7 9 
2 4 6 
2 4 8 
2 1 17 
5 1 15 
5 5 1 
6 17 4 
6 4 17 
+1

'reqNode'에 대한 코드를 보여주십시오. –

+0

어쩌면 나는 눈이 멀지 만 어디에도 'bool'이 보이지 않는다. – twalberg

답변

3

2050xCD입니다. 이는 일반적으로 초기화되지 않은 변수를 사용하고 있음을 의미합니다.

원래 질문의 코드를 기반으로 복사 생성자 reqNodedirection을 복사해야합니다. 결과에 따라 복사되지 않았습니다.

또한 벡터가 vector<reqNode> 인 것으로 표시되므로 임시 reqNodenew과 할당 할 필요가 없습니다. 그냥 스택에 만들고이를 requests.push_back으로 전달하십시오.

0

request.push(*temp)temp이 가리키는 개체의 복사본을 만듭니다. 작풍의 문제로, 당신은 이것을 한 후에 포인터를 지워야한다. 왜냐하면 그 객체는 더 이상 필요하지 않기 때문이다. 더 나은 점은, 그것을 새 것으로 만드는 대신 자동 객체로 생성하는 것입니다. C++은 Java가 아닙니다.

저장되는 사본의 원본과 다른 값이 있으므로 reqNode의 복사 생성자가 올바르게 복사되지 않았 음을 나타냅니다.