错误C2679:二进制'<<' :找不到哪个运算符采用了'Car'类型的右手操作数(或者没有可接受的转换)

时间:2013-01-13 04:04:00

标签: runtime-error

队列类

   #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:二进制'&lt;&lt;' :找不到哪个运算符采用了'Car'类型的右手操作数(或者没有可接受的转换) 我得到这个错误,这有点奇怪。那有什么不对吗?谢谢!

1 个答案:

答案 0 :(得分:1)

cout不知道如何处理car对象;它从未见过car对象,也不知道如何将car作为文本输出。 cout只能处理它所知道的类型,stringcharint等。具体错误是因为存在运算符<<的版本ostreamcar

有两种选择:

  1. operator<<创建一个需要ostreamcar的重载。这将显示如何输出car的cout。通常不会这样做,因为通常有多种方式可以显示汽车。
  2. 编写输出语句,以便手动打印出类似的汽车属性 cout << c.getMake() << " " << c.getModel()