Передача узла в качестве оператора опережения - PullRequest
0 голосов
/ 17 июля 2011

Это печатает сообщение об ошибке о квалификаторах, но вы не понимаете, что это значит и как настроить код для его работы? В любом случае, большое спасибо за просмотр кода.

Примечание: оператор ostream находится в классе Node.

using namespace std;

ostream& operator(ostream& output, const Node* currentNode)
{
   return output;
}

void Node::nodeFunction()
{
   //This node has items attached to the 'this' statement. After
   //the necessary functions, this is called to output the items.

   cout << this;
}

Ответы [ 2 ]

0 голосов
/ 17 июля 2011

Ваше объявление оператора перегруженного потока должно быть таким:

std::ostream& operator<<(std::ostream& os, const T& obj);
^^^^^^^^^^^^^

Вы должны возвращать ссылку на объект std::ostream, & неправильно помещен в прототип вашей перегруженной функции.

Взгляните на рабочий образец здесь .

Для полноты добавьте сюда исходный код.
Примечание: Я взял участников класса Node как общедоступные для простоты демонстрации.

#include<iostream>

using namespace std;

class Node
{
    public: 
    int i;
    int j;
    void nodeFunction();

    friend ostream& operator <<(ostream& output, const Node* currentNode);     
};

ostream& operator<<(ostream& output, const Node* currentNode)
{
   output<< currentNode->i;
   output<< currentNode->j;

   return output;
}

void Node::nodeFunction()
{
   //This node has items attached to the 'this' statement. After
   //the necessary functions, this is called to output the items.

   cout << this;
}

int main()
{
    Node obj;
    obj.i = 10;
    obj.j = 20;

    obj.nodeFunction();

    return 0;
}
0 голосов
/ 17 июля 2011

& в возвращаемом значении оператора находится не в том месте, и обычно лучше использовать ссылки, а не указатели для операторов ostream:

ostream& operator<<(ostream &output, const Node &currentNode)
{
    // Output values here.
    return output;
}

void Node::nodeFunction()
{
     cout << *this;
}
...