开发者

Operator Overload << in Linked List

How can I overload the operator <<. The purpose of the overloaded operator is to do: cout << ptr->info and not receive the memory address but Display the maker year and model of that node's info section.

Example:

template <class DataType>
struct Node {
DataType info;
Node<DataType> *next;
};

In each info section of the Node there will be a struct like this:

struct Car {
    string maker;
    string year;
    string model;
}

So far I have this but it does开发者_运维知识库n't seems to work:

friend ostream &operator << ( ostream &output, Node<DataType> &rlist ) { //Overloaded <<
    output << rlist->info.make << endl;
    output << rlist->info.year << endl;
    output << rlist->info.price << endl; 

    return output;
}  

When I compile with g++ I get this error:

LinkedList.cpp: In member function ‘void LinkedList<DataType>::EscribaUltimo() [with DataType = CarType]’:
main.cpp:37:   instantiated from here
LinkedList.cpp:15: error: no match for ‘operator<<’ in ‘std::cout << ptr->Node<CarType>::info’


Although I'm a bit confused, because you're actual main code is missing. I'm going to assume you have a node, from traversing the link, and now want to print it:

#include <iostream>
#include <string>

using namespace std; // not recommended, but useful
                     // in snippets

// T is usually used, but this is of course up to you
template <class T> 
struct Node
{
    typedef T value_type; // a usual typedef

    value_type info;
    Node<value_type> *next;
};

struct Car
{
    string maker;
    string year;
    string model;
}; // you had a missing ;, probably copy-paste error

// this creates a node. normally you'd want this to be
// wrapped into a list class (more on this later)
template <typename T>
Node<T> *createNode(const T& info = T())
{
    // allocate node
    Node<T> *result = new Node<T>;
    result->info = info;
    result->next = 0; // no next

    return result; // returning a pointer means
                   // whoever gets this is
                   // responsible for deleting it!
}

// this is the output function for a node
template <typename T>
std::ostream& operator<<(std::ostream& sink, const Node<T>& node)
{
    // note that we cannot assume what node contains!
    // rather, stream the info attached to the node
    // to the ostream:
    sink << node.info;

    return sink;
}

// this is the output function for a car
std::ostream& operator<<(std::ostream& sink, const Car& car)
{
    // print out car info
    sink << "Make: " << car.maker <<
            "\nYear: " << car.year <<
            "\nModel: " << car.model << std::endl;

    return sink;
}

int main(void)
{
    // a car list
    typedef Node<Car> CarList;

    // a couple cars
    Car car1 = {"Dodge", "2001", "Stratus"};
    Car car2 = {"GMan's Awesome Car Company", "The future", "The best"};

    CarList *head = createNode(car1); // create the first node
    head->next = createNode(car2);

    // now traverse the list
    CarList *iter = head;
    for (; iter != 0; iter = iter->next)
    {
        // output, dereference iterator to get the actual node
        std::cout << "Car: " << *iter << std::endl;
    }

    // dont forget to delete!
    iter = head;
    while (iter)
    {
        // store next
        CarList *next = iter->next;

        // delete and move on
        delete iter;
        iter = next;
    }
}

Now, if you don't have to create your own linked list, use the standard link list instead, it simplifies your task immensely:

#include <algorithm>
#include <iostream>
#include <iterator>
#include <list>
#include <string>

using namespace std;

struct Car
{
    string maker;
    string year;
    string model;
};


// this is the output function for a car
std::ostream& operator<<(std::ostream& sink, const Car& car)
{
    // print out car info
    sink << "Make: " << car.maker <<
            "\nYear: " << car.year <<
            "\nModel: " << car.model << std::endl;

    return sink;
}

int main(void)
{
    // a car list
    typedef std::list<Car> CarList;

    // a couple cars
    Car car1 = {"Dodge", "2001", "Stratus"};
    Car car2 = {"GMan's Awesome Car Company", "The future", "The best"};

    CarList cars;
    cars.push_back(car1);
    cars.push_back(car2);

    // now traverse the list (copy to ostream)
    std::copy(cars.begin(), cars.end(),
             std::ostream_iterator<Car>(std::cout,"\n"));

    // delete done automatically in destructor
}

Hope this helps.


Just to make sure, you have

template<class DataType>

before the operator definition, right? If I do that, it works for me. The error messages show the line numbers in your code, but where is it in the pasted definitions? Reading it, I think the problem isn't with

Node<DataType> myNode;
output << myNode 

but with

output << myNode.info

which does not have a operator<< defined for it.

Edit: By your comment, it sounds like you want to define an << operator for the car. So, I would do (untested)

ostream& operator<< (ostream& output, Car& car) {
  output << car.foo << end;
  output << car.bar << end;
  return output;
}

template <class DataType>
ostream& operator<< (ostream& output, Node<DataType>& node ) {
  output << node.info << end;
  return output;
}

Basically, what that means is that when you specialize your Node type and wish to use the << operator on it, you need to make sure that the DataType you are specializing with also has the << operator defined.


You need two operators: one for Node (or Node*), and one for Car:

ostream &operator << ( ostream &output, Car &car ) {
    output << car.maker << endl;
    output << car.year << endl;
    output << car.model << endl; 
    return output;
}

template<class DataType>
ostream &operator << ( ostream &output, Node<DataType> *rlist ) {
    output << rlist->info;
    return output;
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜