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.