0

队列类

   #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: binary '<<' : no operator found 它采用'Car' 类型的右手操作数(或没有可接受的转换)我得到这个错误,这有点奇怪。那么有什么问题吗?谢谢!

4

1 回答 1

1

cout不知道如何处理car对象;它从未见过car对象,也不知道您如何将 a 输出car为文本。 cout只能处理它知道的类型string,, char, int, 等。具体的错误是因为存在<<接受 anostream和 a的运算符版本car

有两种选择:

  1. 创建一个operator<<需要 aostream和 a的重载car。这将显示 cout 如何输出一个car. 这通常不会这样做,因为您想要展示汽车的方式通常不止一种。
  2. 编写输出语句,以便手动打印出汽车属性,例如 cout << c.getMake() << " " << c.getModel()
于 2013-01-13T04:12:52.823 回答