i have overloaded the '<<' operator in the linked class implementation, and i declared the .cpp file in my main function, but it's not working, and this error occurs: undefined reference to operator<& list)
this is the declaration of the ostream function in linkedlist.h file:
friend std::ostream& operator<< (std::ostream& os, LinkedList<T>& list);
and this is the implementation of the ostream function:
template <typename T>
std::ostream& operator<< (std::ostream& os, LinkedList<T> list)
{
list.current = list.start;
while(list.current != NULL)
{
os<< list.current->info<<" -> ";
list.current = list.current->next;
}
os<<"NULL"<<endl;
return os;
}
In the main function, I have created a list that has object from SavingAccount class
LinkedList <SavingAccount> list;
and the error occurs in the main function in this line:
cout << list <<endl;
well.. this is the LinkedList class implementation:
#include "LinkedList.h"
#include "SavingAccount.h"
#include <cstddef>
using namespace std;
template <typename T>
LinkedList<T>::LinkedList()
{
start = NULL;
current = NULL;
}
template <typename T>
LinkedList<T>::~LinkedList()
{
// Add code.
}
template <typename T>
std::ostream& operator<< (std::ostream& os,const LinkedList<T>& list) {
list.current = list.start;
while(list.current != NULL)
{
os<< list.current->info<<" -> ";
list.current = list.current->next;
}
os<<"NULL"<<endl;
return os;
}
and this is the header file of LinkedLins class:
#ifndef LINKEDLIST_H
#define LINKEDLIST_H
#include<iostream>
using namespace std;
template <typename T>
struct Node{
T info;
Node<T> *next;
};
template <typename T>
class LinkedList
{
Node<T> *start;
Node<T> *current;
public:
LinkedList();
~LinkedList();
friend std::ostream& operator<< (std::ostream& os, const LinkedList<T>& list);
};
#endif // LINKEDLIST_H
i hope you guys can help me with that, your help is much appreciated