c++ - 具有自定义数据结构的 C++ 中的运算符<<

标签 c++ oop object c++11

我正在尝试制作一组​​自定义头文件以使用通用 List 并使用 operator<< 使其打印到 ostream 对象中 有几个错误我无法解决。

我正在尝试使用通用 Node 类创建通用 List 类。应该使用 operator<< 打印通用列表。但是我遇到了很多错误。

#include <iostream>
using std::cout;
using std::ostream;

class List;

template<class T>
class Node
{
    friend class List<Node>;
    private:
    T data_;
    Node *next_;

    public:
    Node(T data);
    T get_data();
};

template<typename T>
Node<T>::Node(T data)
{
    data_ = data;
    next_ = 0;
}

template<typename T>
T Node<T>::get_data()
{
     return data_;
}

template<typename T>
class Node;

template<typename T>
class List
{
    template<typename T>
    friend ostream& operator<<(ostream& o , const List<T> head);

    private:
    Node<T> *start_;
    bool is_empty();

    public:
    List();
    ~List();
    void insert(T data);
    void remove();  
};

template<typename T>
bool List<T>::is_empty()
{
    if(start_ == 0)
        return true;        
    return false;
}

template<typename T>
List<T>::List()
{
    start_ = 0;
}

template<typename T>
List<T>::~List()
{
    if( !is_empty())
    {
        Node<T> *current = start_;
        Node<T> *temp;
        while(current != 0)
        {
            temp = current;
            current = current->next_;
            delete temp;
        }
    }
}

template<typename T>
void List<T>::insert(T data)
{
    if(is_empty())
    {
        Node<T> *temp = new Node<T>(data);
        start_ = temp;
    }
    else
    {
        Node<T> *temp = start_;
        while(temp->next_ != 0)
        {
            temp = temp->next_;
        }
        temp->next_ = new Node<T>(data);
    }
}

template<typename T>
void List<T>::remove()
{
    if(start_->next_ == 0)
    {
        Node<T> *temp = start_->next_;
        start_->next_ = 0;
        delete temp; 
    }
    else if(!is_empty())
    {
        Node<T> *stay = start_;
        Node<T> *remove = stay->next_;
        while(remove->next_ != 0)
        {
            stay = remove;
            remove = remove->next_;
        }
        stay->next_ = 0;
        delete remove;
    }
}


 // Experimental Stuff out here
template<typename T>
ostream& operator<<(ostream& o , const List<T> *head)
{
    Node<T> *temp = head->start_;
    if(!is_empty<T>())
    {
        while(temp->next_ != 0)
        {
            o << temp.get_data() << "\t";
            o << "\n";
            temp = temp->next_;
        }
    }
    return o;
}
//  End of Experimental Stuff

最佳答案

假设您的类的客户像这样实例化它:

List<int> my_list;

您需要定义您的 operator <<像这样重载:

template <typename T>
std::ostream& operator << (std::ostream& os, const List<T>& list)
{
    // I'm gonna assume you have C++11
    auto node = list.start_;
    while (node != nullptr)
    {
        os << '\t' << node->get_data() << '\n';
        node = node->next_;
    }
    return os;
}

此外,您的代码中有大量关于模板的错误,因此请先修复它们。

关于c++ - 具有自定义数据结构的 C++ 中的运算符<<,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29456138/

相关文章:

javascript - 循环无限嵌套对象并构造字符串

c++ - PhysX:关节摩擦/"stiff"关节

c++ - cuda数组排序推力,内存不足

javascript - 做点什么并继续上课

PHP 手册 OOP 可见性示例 - 有人可以解释一下吗

java - 在 Java 中复制后保留对象的值

javascript - 将对象中的数据类型从 int 转换为 string

c++ - 这会在哪些平台上崩溃,我该如何改进它?

c++ - boost::program_options 错误或功能?

C# 主窗体实现接口(interface)