2013-01-13 48 views
0

Queue類錯誤C2679:二進制「<<」:沒有操作員發現這需要類型的右邊的操作數「汽車」(或沒有可接受的轉化率)

#ifndef Queue_H 
    #define Queue_H 

    #include "Car.h" 
    #include <iostream> 
    #include <string> 

    using namespace std; 

    const int Q_MAX_SIZE = 20; 

    class Queue { 
    private: 

     int size; // size of the queue 
     Car carQueue[Q_MAX_SIZE]; 
     int front, rear; 
    public: 
     Queue(); 
     ~Queue(); 
     bool isEmpty(); 
     bool isFull(); 
     void enqueue(Car c); 
     void dequeue(); // just dequeue the last car in the queue 
     void dequeue(Car c); // if a certain car wants to go out of the queue midway. 
          // Condition: Car is not in washing. Meaning is not the 1st item in the queue 
     void dequeue(int index); // same as the previous comment 
     Car getFront(); 
     void getCarQueue(Queue); 
     int length(); 
     Car get(int); 
    }; 

    Queue::Queue() { 
     size = 0; 
     front = 0; 
     rear = Q_MAX_SIZE -1; 
    } 

    Queue::~Queue() { 
     while(!isEmpty()) { 
      dequeue(); 
     } 
    } 

    void Queue::enqueue(Car c) { 
     if (!isFull()) { 
      rear = (rear + 1) % Q_MAX_SIZE; // circular array 
      carQueue[rear] = c; 
      size++; 
     } else { 
      cout << "Queue is currently full.\n"; 
     } 
    } 

    void Queue::dequeue() { 

    } 

    void Queue::dequeue(int index) { 
     if(!isEmpty()) { 
      front = (front + 1) % Q_MAX_SIZE; 
      if(front != index) { 
       carQueue[index-1] = carQueue[index]; 
       rear--; 
       size--; 
      } else { 
       cout << "Not allowed to dequeue the first car in the queue.\n"; 
      } 
     } else { 
      cout << "There are no cars to dequeue.\n"; 
     } 
    } 

    bool Queue::isEmpty() { 
     return size == 0; 
    } 

    bool Queue::isFull() { 
     return (size == Q_MAX_SIZE); 
    } 

    Car Queue::getFront() { 
     return carQueue[front]; 
    } 

    int Queue::length() { 
     return size; 
    } 

    Car Queue::get(int index) { 
     return carQueue[index-1]; 
    } 

    void Queue::getCarQueue(Queue q) { 
     for(int i = 0; i< q.length(); i++) 
      cout << q.get(i) << endl; // Error here 
    } 

    #endif 

錯誤C2679:二進制「< <':找不到操作符找到'Car'類型的右側操作數(或者沒有可接受的轉換) 我得到這個錯誤,有點奇怪。那麼有什麼不對嗎?謝謝!

回答

1

cout不知道如何處理一個car對象;它從未見過car對象,並且不知道如何輸出car作爲文本。 cout只能處理它所知道的類型,string,char,int等。具體的錯誤是因爲存在操作符<<的版本,其需要ostreamcar

有兩種選擇:

  1. 創作operator<<的重載需要一個ostreamcar。這將顯示如何輸出car。通常不會這樣做,因爲通常有多種方法可以顯示汽車。
  2. 寫輸出語句,以便它手動打印出汽車性能,如 cout << c.getMake() << " " << c.getModel()
+0

感謝您的指導!它適用於否。 2 :) – unknown

相關問題