如何重载<<嵌套类中的运算符

时间:2014-08-21 15:09:16

标签: c++ linked-list operator-overloading

我有三个类:Client DatabaseNode作为Database中的嵌套类。

Node的数据是指向Client对象的指针,Client类有自己的重载<<运算符。

我需要一个重载的<<运算符,它将输出所有链表的数据。 我遇到的问题是无法使用<<重载运算符遍历所有列表,我能做的最好的事情是使用getData成员输出头节点的数据,对于某些原因Node::Print不会输出所有列表Client *data

以下是Database类以及上述两种方法<<print()

Databse.h

class Databse
{
    private:
        class Node //node as nested class
        {
            public:
                Node();
                void setNode(Client*&);
                Node* nextNode(Node*&);
                Client getData();
                void print (Node*);
            private:
                Client* data; //holds pointer to Client object
                Node* next; //holds pointer to next node in list
        };

        Node *head; //holds the head node
        int nClients;

    public:
        Databse();
        friend ostream& operator<<(ostream&, const Databse&);
        Node* getHead() const;
        ~Databsey();
};

Databse.cpp相关方法:

ostream& operator<<(ostream& out, const Databse& obj)
{   
    out << endl << "The databse holds" << obj.nClients << " clients:" << endl;
    out << obj.head->getData();        
    obj.head->print(obj.getHead());
    return out;
}

void Database::Node::print (Node* str)
{
    Node* current = str ;
    while (current->next)
    {
        cout << current->data;
        current = current->next;
    }
}

感谢。

3 个答案:

答案 0 :(得分:2)

只需添加重载的operator<<,就像添加任何其他类一样,例如:

class Databse
{
    private:
        class Node //node as nested class
        {
            public:
                Node();
                void setNode(Client*&);
                Node* nextNode(Node*&);
                Client getData();
                friend ostream& operator<<( ostream&, const Node*& pNode );
            private:
                Client* data; //holds pointer to Client object
                Node* next; //holds pointer to next node in list
        };

        Node *head; //holds the head node
        int nClients;

    public:
        Databse();
        friend ostream& operator<<(ostream&, const Databse&);
        friend ostream& operator<<( ostream&, const Node*& pNode );
        Node* getHead() const;
        ~Databsey();
};

然后按如下方式实施:

ostream& operator<<( ostream& out, const std::add_lvalue_reference<Databse::Node*>::type pNode )
{
    Databse::Node* pCurrNode = pNode;
    while( pCurrNode )
    {
        std::cout << pCurrNode->data;
        pCurrNode = pCurrNode->next;
    }

    return out;
}

答案 1 :(得分:2)

另一种方法。

  1. 保持Database::Node:print()非常简单 - 仅在对象上打印数据。
  2. ostream& operator<<(ostream& out, const Database& obj)负责迭代列表中的节点并打印每个节点。
  3. 这是更新的代码。

    class Database
    {
       private:
          class Node //node as nested class
          {
             public:
                Node();
                void setNode(Client*&);
                Node* nextNode(Node*&);
    
                // Need this to allow iteration of the list of nodes
                // of a Database object.
                Node* nextNode() const
                {
                   return next;
                }
                Client getData();
    
                // Print the data associated with just one node.
                void print(ostream& out) const
                {
                   cout << data;
                }
    
             private:
                Client* data; //holds pointer to Client object
                Node* next; //holds pointer to next node in list
          };
    
          Node *head; //holds the head node
          int nClients;
    
       public:
          Database();
          friend ostream& operator<<(ostream&, const Database&);
          Node* getHead() const;
          ~Database();
    };
    
    ostream& operator<<(ostream& out, const Database& obj)
    {   
        out << endl << "The databse holds" << obj.nClients << " clients:" << endl;
    
        // Iterate over the nodes of the Database object
        // and print each one.
        Database::Node* current = obj.head;
        for ( ; current != NULL; current = current->nextNode() )
        {
           current->print(out);
           // Printing a whitespace between nodes makes the
           // output more readable.
           out << " ";
        }
    
        return out;
    }
    

答案 2 :(得分:1)

修复接受ostream的打印

ostream& Database::Node::print (ostream& out, Node* str)
{
  Node* current = str ;
  while (current) //bugfix
  {
    out << current->data;
    current = current->next;
  }
  return out
}

然后你可以打电话

ostream& operator<<(ostream& out, const Databse& obj)
{   
  out << endl << "The databse holds" << obj.nClients << " clients:" << endl;
  out << obj.head->getData();        
  obj.head->print(out,obj.getHead());
  return out;
}
相关问题