0

For an assignment (and for myself) I need to be able to print a singlely linked list of "Stock" objects.

Each Stock as the following private data members: 1 int 1 string

This is my print function in the linked list class I had to use/develop. I am not allowed to use STL::list().

//list.h
template<class NODETYPE>
void List< NODETYPE>::print() const
{
if(isEmpty() ){
    cout<<"the list is empty\n\n";
    return;
}

ListNode< NODETYPE> *currentPtr=firstPtr;

cout<<"The list is: ";

while (currentPtr!=0){
    //cout << currentPtr->data << ' ';
    currentPtr = currentPtr->nextPtr;
}

cout<< "\n\n";
}

Now for the Nodes in the List, listnode.h:

//ListNode.h
//ListNode Template Definition
#ifndef LISTNODE_H
#define LISTNODE_H

template< class NODETYPE > class List;

template < class NODETYPE>
class ListNode {
friend class List < NODETYPE>;

public:
    ListNode (const NODETYPE &); //constructor
    NODETYPE getData() const;

private:
   NODETYPE data;
   ListNode< NODETYPE> *nextPtr;

};

 template<class NODETYPE>
 ListNode< NODETYPE >::ListNode(const NODETYPE &info): data (info), nextPtr(0)
 {

 }
 template<class NODETYPE>
 NODETYPE ListNode<NODETYPE>::getData() const{
    return data;
 }

#endif

Now my Stock Class:

class Stock{
public:
    Stock(); //default constructor
    Stock(string, int); //overloaded constructor
    ~Stock(); //deconstructor
    void setSymbol(string); //sets stock symbol
    void setShares(int);
    string getSymbol();
    int getShares();
private:
    string symbol;
    int shares;
};
Stock::Stock(){
symbol=" ";
shares=0;
}
Stock::Stock(string s, int num){
    cout<<"Creating Stock with supplied Values! \n\n"<<endl;
    symbol=s;
    shares=num;
    price=p;
}

Simple int main() for testing:

int main(){

List < Stock > portfolio_list;

Stock Google("GOOG", 500);
Stock VMware("VMW", 90);
Stock Apple("APL", 350);

portfolio_list.insertAtFront(Google);
portfolio_list.insertAtFront(VMware);
portfolio_list.insertAtFront(Apple);

portfolio_list.print();

return 0;

}

So obviously, I get operator errors for "<<" in list.h, because it can't output the contents of my stock class. My end goal is to loop through every element in a linked list and print the string symbol and int. I'm assuming I will need to use some sort of operator overloading? Or am I way off base? If I do, should this be implemented in my Stock class? Thanks in advance.

3 Answers 3

3

You will need to overload the << operator.

Sign up to request clarification or add additional context in comments.

Comments

1

If I understood your code, you'll have to overload the << operator to both ListNode and List classes.

//Representation of ListNode
template <class T>
ostream& operator << (ostream& ostr, ListNode<T>& ls) {
    return ostr << "Foo"; //replace "Foo" with the proper representation
}

//Representation of List
template <class T>
ostream& operator << (ostream& ostr, List<T>& ls) {
    ostr << "[ ";
    for(ListNode<T> *i = ls.firstPtr; i != NULL; i = i->nextPtr)
        ostr << *i << " ";
    return ostr << "]";
}

Would print [ Foo Foo Foo ] for a 3 element list.

BTW, you should mark those functions as friend if you need to access private members.

2 Comments

so instead of return ostr << "Foo"; it should represent calls to my template'd object? I don't quite follow how to get an integer from my stock object from there then. If I have an Object 'Stock' and I want to stream out an 'int Num' and 'String Name' from it.
@Staypuft the representation of a Stock object could be ostr << "(" << s.symbol << " " << s.shares << ")" and the result should be [ (GOOG 500) (VMW 90) (APL 350) ]
1

I think the answer could be more simple. You should only have to just override the << operator the Stock class.

Since your List's print() function is set to point at each node, and then cout << currentPtr->data, the only thing that needs to have an operator overload is each individual "data" class.

In this case, that is your Stock class.

So in order to be able to perform

    cout << currentPtr->data << ' ';

you basically also need to be able to do

    int main(){

        Stock Google("GOOG", 500);
        cout << Google << ' ';
    }

That way, all your list needs to do is cout the class occupying "data" in your list, in this case Stocks.

SO

your Stocks class needs to have an operator overload similar to what was mentioned above. It could look something like this:

//Representation of Stock
ostream& operator << (ostream& ostr, Stock &st) {
    return ostr << "stuff"; // "stuff" is your data (st.symbol and whatever else)
}

That way you only have to override the << operator once! :)

1 Comment

Oh, didn't notice the date on this. ... I'm a little overdue. Haha, well then this probably won't have much use, but hopefully it's still helpful!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.