Hello,
Basically I'm trying to use operator<< to display items from a linked list.
coding in the header file.
template <class DataType>
struct Node {
DataType info;
Node<DataType> *next;
};
template <class DataType> class LinkedList;
template <class DataType>
ostream & operator<<(ostream & output, const LinkedList<DataType> & rlist);
template <class DataType>
class LinkedList
public:
friend ostream & operator<< <>(ostream & output, const LinkedList<DataType> & rlist);
then the implementation is
template<class DataType>
ostream & operator<<(ostream& output, const LinkedList<DataType> rlist)
{
Node<DataType> *ptr; //pointer to traverse the list
ptr = rlist.start; //set current so that it points to
//the first node
while(ptr != NULL) //while more data to print
{
output << current->info <<" ";
ptr = ptr->info;
}
return output;
}
and when I try to display items in the main
#include <iostream>
using namespace std;
struct Account
{
int accountNumber;
float accountBalance;
string customerName;
string customerAddress;
};
int main()
{
// 9 is int id, float is 2000, string Name, string City
Account ac1 = {9, 2000, "myname", "city"};
LinkedList<Account> accounts;
accounts.insert(ac1);
cout << accounts;
return 0;
}
I bet I'm doing something stupid, so please help and explain if I'm doing something wrong.
Thanks.