c++ - 链表中的运算符重载 <<

标签 c++ operator-overloading linked-list

如何重载 operator << . 重载运算符的目的是: cout << ptr->info并且不接收内存地址,但显示该节点信息部分的制造商年份和型号。

例子:

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

在节点的每个信息部分都会有一个这样的结构:

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

到目前为止我有这个但它似乎不起作用:

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;
}  

当我用 g++ 编译时,我得到这个错误:

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’

最佳答案

虽然我有点困惑,因为你缺少真正的主要代码。我将假设您有一个节点,通过遍历链接,现在想要打印它:

#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;
    }
}

现在,如果您不必创建自己的链表,请改用标准链表,它会极大地简化您的任务:

#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
}

希望这对您有所帮助。

关于c++ - 链表中的运算符重载 <<,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1558208/

相关文章:

haskell - 重载 Haskell 中的内置函数

C++,指针数组,指向双向链表中的条目

c++ - 如何在 C++ 中检查整数单链表的对称性?

c++ - 为什么 std::set::erase 与 std::set::insert 不一致?

C++ STL : Why is there no upper_bound equivalent that retrieves the greatest element smaller then a specific key?

c++ - 类数组成员的初始化

c++ - 我的代码是在创建对象的深拷贝还是浅拷贝?

C++ 在范围结束前删除函数指针

c++ - 有没有办法重载 operator= 以对右侧的函数具有特定行为

c++ - 重载矩阵运算符