CowEatsGrass
CowEatsGrass

Reputation: 41

How to display from a linked list with a class as template type?

So I have the following class:

class Message
{
private:
    vector<string> messageLines;
    int numLines;
public: 
    void setNumLines(int nLines) {numLines = nLine;);//setting number of lines in the message
    bool setMessage(int lineNum, string message);//fills up the vector
    ostream& writeMessage(ostream &os)// overloding << operator, displays the vector.
}
bool Message :: setMessage(int lineNum, string message)
{
     if (lineNum > 0)
     {
         messageLines.push_back(message);
         return true;
     }
}
ostream& Message:: writeMessage(ostream &os)
{
    for (int i = 0 ; i < messageLines.size() ; i++)
    {
        os << messageLines[i] << endl;
    }
    return os;
}

From my main, after I declared a LinkedList<Message> variable and a Message messageVar to fill up the vector, I inserted the Message variable into a linked list like this:

variable.insert(messageVar);

Here is an example of what my main looks like:

int main()
{
    LinkedList<message> variable;

    for (int i = 0 ; i < 5 ; i++)
    {
        Message messageVariable;
        messageVariable.setMessage(1, "random strings") 
        variable.insert(messageVariable) // filling up the list with 5 random strings
    }
}

However, when I try to call the linked list's display function and try to display the string values, i realized that the cout << currPtr->getItem() in the display function will not work since it I am displaying the Message type rather than a string. I know I should use the overloaded << operator from the class I wrote but I have no idea where I should use it.

Here is the display function just in case:

template<class ItemType>
void LinkedList<ItemType>::display() const
{
    Node<ItemType>* currPtr = headPtr;//The Node<ITemType> is in another header file
    while (currPtr->getNext() != headPtr)                   
    {
        cout << currPtr->getItem() << endl; // cout can't display Message type
        currPtr = currPtr->getNext();       
    }
    cout << endl << endl;
}

Any suggestions?

Upvotes: 0

Views: 154

Answers (1)

vsoftco
vsoftco

Reputation: 56557

You can overload operator<< in Message

friend ostream& operator<<(ostream& os, const Message& rhs)
{
    return rhs.writeMessage(os);
}

Or, you can try a direct call to writeMessage in display(), such as:

currPtr->getItem().writeMessage(std::cout) << std::endl; // less elegant

PS:

ostream& writeMessage(ostream &os)// overloding << operator, displays the vector.

is not overloading operator<<, but it is just a helper function. You don't really need it here; it is a good idea to have such a function if you have a class hierarchy in which you mark it virtual and call it inside operator<<. This way, you have dynamic binding via a non-virtual (in this case non-member) function. This idiom is called NVI (non-virtual interface), and it is useful because it lets you define a mandatory implementation (in this case operator<<), which can call the "right" display function at run-time. So basically each derived class overrides writeMessage, but there is only one operator<< defined in the base class which "knows" to call the right writeMessage.

Upvotes: 1

Related Questions