Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ Operator '<<' error

Tags:

c++

I have a question regarding a homework assignment.

I have two classes. One is called ticket.cpp, and the other is called TicketOrder.cpp

The main is within the ticket.cpp.

I am using a g++ compiler on Linux.

What I'm doing is trying to is print out a vector of a TicketOrder object called orders, but it gives me the following error:

ticket.cpp:57: error: no match for 'operator<<' in 'std::cout << orders. std::vector<_Tp, _Alloc>::operator[] with _Tp = TicketOrder, _Alloc = std::allocator'

Here is my code:

ticket.cpp

#include <iostream>
#include <vector>
#include <limits>
#include <cctype>
#include "TicketOrder.cpp"
using namespace std;

int main ()
{
    int numberoftickets=0;
    string input2;
    char input3;
    int profit=0;
    vector <TicketOrder> orders;
    int atotalmoney=0;
    int btotalmoney=0;
    int ctotalmoney=0;
    int dtotalmoney=0;
    int etotalmoney=0;

    do
    {
        cout << "\nPick a ticket that you would like to buy: \n\n";
        cout << "(A) Students without an activity card: $2.00 \n";
        cout << "(B) Faculty and staff: $3.00 \n";
        cout << "(C) USC alumni: $5.00 \n";
        cout << "(D) UCLA students and alumni: $20.00 \n";
        cout << "(E) Everyone else: $10.00 \n";

        cin >> input3;

        if (input3=='A')
        {
            cout << "How many tickets do you wish to buy? " <<endl;

            if (numberoftickets >0)
            {
                TicketOrder order;
                order.setQuantity(numberoftickets);
                order.setType(input3);
                orders.push_back(order);
                for (int i=0; i< orders.size(); i++)
                {
                    cout << orders[i];
                }
            }
        }
        else
        {
            cout << "Sorry did not recognize input, try again. " << endl;
        }
    } while (input3 != 'S');

TicketOrder.cpp:

#include <iostream>
using namespace std;

class TicketOrder
{
public :
    //Getters

    int getQuantity() const
    {
        return quantity;
    }

    char  getType() const
    {
        return type;
    }

    //Setters

    void setQuantity (int x)
    {
        quantity=x;
    }

    void setType(char y)
    {
        type =y;
    }

private:
    char type;
    char quantity;

};
like image 282
r1nzler Avatar asked Feb 08 '12 09:02

r1nzler


People also ask

What is C programming error?

Errors in C language is defined as an illegal operation performed by the user which will result in the abnormal or abrupt working of the program logic. Programming errors are unidentified until the program is compiled or executed. Some of the errors in C are hidden or prevent the program from compiled or executed.

How does C handle errors?

As such, C programming does not provide direct support for error handling but being a system programming language, it provides you access at lower level in the form of return values. Most of the C or even Unix function calls return -1 or NULL in case of any error and set an error code errno.

Can you throw errors in C?

The C programming language does not support exception handling nor error handling. It is an additional feature offered by C. In spite of the absence of this feature, there are certain ways to implement error handling in C. Generally, in case of an error, most of the functions either return a null value or -1.


1 Answers

As the compiler is clumsily trying to explain, the code is missing an operator<< for the TicketOrder class.

class TicketOrder {
public:
    friend std::ostream& operator<<(std::ostream& os, TicketOrder const& order) {
        os << "Type: " << type << ", quantity: " << quantity;
        return os;
    }

    char type;
    int quantity;
};

(Note: you probably want to change quantity to int.)

like image 92
R. Martinho Fernandes Avatar answered Oct 10 '22 11:10

R. Martinho Fernandes