6

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;

};
Bill the Lizard
  • 398,270
  • 210
  • 566
  • 880
r1nzler
  • 2,533
  • 8
  • 28
  • 30

3 Answers3

7

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.)

Community
  • 1
  • 1
R. Martinho Fernandes
  • 228,013
  • 71
  • 433
  • 510
  • HH, I didn't now that `operator<<(std::ostream&& os, ...)` can be defined in class like that. Btw: it should be `const TicketOrder &` instead of `TicketOrder const` – Vyktor Feb 08 '12 at 09:43
  • @Vyktor: check again. `TicketOrder const&` is exactly the same. – R. Martinho Fernandes Feb 08 '12 at 09:44
  • `TicketOrder const` and `const TicketOrder` is equivalent syntax? Good to know, thanks. – Vyktor Feb 08 '12 at 09:49
  • 1
    Style advice, rather than have a `friend` operator, have a normal member function `print(std::ostream&)const` and have a non-friend global `operator<<` call that. Looser coupling, and you can make `print` more flexible (extra parameters with defaults, say). – spraff Feb 08 '12 at 10:07
  • @R.MartinhoFernandes Doesn't `TicketOrder const&` mean a "a constant pointer to a variable value", whereas `const TicketOrder&` means "a variable pointer to a constant value"? Big difference there. – Zenexer Feb 09 '12 at 03:58
  • 1
    @Zenexer: *there are no pointers here*. `TicketOrder const&` means "a reference to a a constant `TicketOrder`", and `const TicketOrder&` means "a reference to a a constant `TicketOrder`". Please consult the nearest C++ book. – R. Martinho Fernandes Feb 09 '12 at 09:25
  • @R.MartinhoFernandes Sorry, I'm stuck in C# land. – Zenexer Feb 09 '12 at 11:58
0

You must add the operator << function as a friend to be able to print values from your TicketOrder objects with cout. Further reading

Alexander
  • 8,117
  • 1
  • 35
  • 46
0

You're attempting to use the << operator on cout and a TicketOrder object. That is undefined. You should use the TicketOrder object to generate a string first, then output that via cout. Either that, or you can define the << operator for the TicketOrder class, as described in one of the other two answers.

Zenexer
  • 18,788
  • 9
  • 71
  • 77