2012-04-16 4 views
1

대기열에 사용 된 배열의 내용을 인쇄하는 데 문제가 있습니다.대기열에 배열 인쇄

내 템플릿 큐의 일부 :

#include <iostream> 
#include <cstdlib> 
using namespace std; 

template<class T> 
class Queue 
{ 
private: 
    int front;  //front position 
    int rear;  //rear position 
    int maxQue;  //maximum number of elements in the queue 
    T* items;  //points to a dynamically-allocated array code here 
public: 
    Queue() // default constructor: Queue is created and empty 
    { 
     front = -1; 
     rear = 0; 
     maxQue = 10; 
     items = new T[maxQue]; 
    } 

    void Print() // print the value of all elements in the queue 
    { 
     while(front != rear) 
     { 
      cout<<items[front]; 
      front++; 
      if(front==rear) 
       break; 
      cout<<" - "; 
     } 
     cout<<endl; 
    } 

    void Enqueue(T add)  // insert x to the rear of the queue 
    {       // Precondition: the queue is not full 
     if(IsFull()) 
     { 
      cout<<"Queue is full!"<<endl; 
     } 
     else 
     { 
      items[rear] = add; 
      rear++; 
      rear = rear % maxQue; 
     } 
    } 

    void Dequeue(T &x) // delete the element from the front of the queue 
    {      // Precondition: the queue is not empty 
     if(!IsEmpty()) 
     { 
      front = (front+1)%maxQue; 
      x = items[front]; 
     } 
    } 

    bool IsEmpty() // test if the queue is empty 
    { 
     return (rear==front); 
    } 

    bool IsFull() // test if the queue is full 
    { 
     return ((rear+1)%maxQue==front); 
    } 

    int length() // return the number of elements in the queue 
    { 
     return abs(rear-front); 
    } 

    ~Queue() // Destructor: memory for the dynamic array needs to be deallocated 
    { 
     delete [] items; 
    } 
}; 

메인 루틴의 일부 :

int main() 
{ 
    Queue<float>FloatQueue; 
    float y; 
    FloatQueue.MakeEmpty(); 

    FloatQueue.Dequeue(y); 
    FloatQueue.Enqueue(7.1); 
    cout << "float length 3 = " << FloatQueue.length() << endl; 

    FloatQueue.Enqueue(2.3); 
    cout << "float length 4 = " << FloatQueue.length() << endl; 

    FloatQueue.Enqueue(3.1); 
    FloatQueue.Dequeue(y); 
    cout << "The float queue contains: "; 
    FloatQueue.Print(); 

    return 0; 
} 

가되는 시점에서 나는 이러한 오류를 얻을 인쇄하려고 할 때까지 코드는 벌금 컴파일 .

0 00000000 0x00466a7f in std::__convert_from_v() (??:??) 
1 00000000 0x00430302 in std::num_put<char, std::ostreambuf_iterator<char, std::char_traits<char> > >::_M_insert_float<double>() (??:??) 
2 00000000 0x00430da8 in std::num_put<char, std::ostreambuf_iterator<char, std::char_traits<char> > >::do_put() (??:??) 
3 00000000 0x00447455 in std::ostream::_M_insert<double>() (??:??) 
4 00000000 0x00448988 in std::ostream::operator<<() (??:??) 
5 0041CB37 Queue<float>::Print(this=0x28ff00) 

저는 며칠 동안이 문제에 봉착했습니다. 도움을 주시면 감사하겠습니다.

+1

#include 을 잊어 버리셨습니까? –

+0

oh yea 그리고'std :: cout'을 사용하고 ** 사용하지 말라.'#using namespace std; ' –

+0

#include 을 가지고 있었고, 내 게시물을 편집 할 것이다. #using 네임 스페이스 std가없는 이점은 무엇입니까? 나는 C++에 다소 익숙하다. –

답변

0

고정 크기 순환 버퍼를 구현하는 것처럼 보입니다. 그렇다면 (또는 그렇지 않더라도) 몇 가지 문제가 있습니다 :

  1. 대기열의 최대 크기를 초과하여 대기열에 넣기 전에 대기열에서 아무것도 가져 오지 않으면 대기열로 등록되지 않습니다.
  2. "앞"포인터가 뒷쪽 포인터보다 큰 경우 인쇄 기능이 멈추지 않고 앞면이 MAX_INT까지 계속 돌아가며 다시 돌아갈 수 있습니다. 최대 크기의 버퍼에서 mod 연산을 수행하지 않습니다.
  3. 소멸자가 없으므로이 객체 중 하나를 만들고 파괴 할 때마다 버퍼가 누출됩니다.
  4. 길이 기능이 잘못되었습니다. 정면이 후방보다 크면 (이는 반 시간이다) 오류가 발생할 것이다. 이 방법으로 생각하면 크기가 가득 차면 크기가 0이됩니다.

아마도 그 외에도 몇 가지가 있습니다. 나는 당신의 디자인을 다시 생각할 것입니다. 당신은 가깝지만 몇 가지 수학 오류가 있습니다.