1

I am trying to figure out what I am doing wrong with this printList Function. I am getting the following compiler error:

No operator "<<" matches these operands.

The function is as follows:

void printList(const List& theList)
{   
for(Node* i = theList.getFirst(); i != theList.getLast(); ++i)
{
    cout << *i << " ";
    cout << endl;
}
}

I have the following as well,

#include "List.h"
#include <iostream>

I am thinking my print function is just way off base. Can anyone point me in the right direction?

Here are my classes, I don't have a List::Iterator. What would you suggest?

class List
{
private:
    int nodeListTotal;
    Node* first;
    Node* last;

public:
    //Constructor
    List();

    void push_back(Node*);
    void push_front(Node*);
    Node* pop_back();
    Node* pop_front();
    Node* getFirst() const;
    Node* getLast() const;
    int getListLength() const;
    void retrieve(int index, int& dataItem) const;
};

class Node
{
private:
    string dataItem;
    string dataUnit;
    int unitTotal;
    Node* next;

public:
    //Constructor
    Node();

    Node(int, string, string);

    string getDescription( );
    void setDescription(string);

    string getQuantityName();
    void setQuantityName(string);

    int getQuantityNumber();
    void setQuantityNumber(int);

    Node* getNext( );
    void setNext(Node*);
};
1
  • 1
    Try something simpler: Node n; cout << n;. If that doesn't work, then you forgot to define ostream &Node::operator<<(ostream &). Commented Aug 5, 2013 at 3:58

3 Answers 3

1

You need to overload operator<< for Node type:

std::ostream& operator<<(std:::ostream& os, const Node& node)
{
    os << node.getQuantityName() << " " << node.getDescription();
    return os;
}
Sign up to request clarification or add additional context in comments.

1 Comment

I have updated the question with my classes I don't have List::Iterator.
0

As the error message says

No operator "<<" matches these operands.

you don't have the << operator defined for your class.

Comments

0

In your printList() function, replace cout << *i << " "; with
cout << i->getDescription() << endl;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.