c++ - i have done the <<operator overloading but it's not working -
i have overloaded '<<' operator in linked class implementation, , declared .cpp file in main function, it's not working, , error occurs: undefined reference operator<& list)
this declaration of ostream function in linkedlist.h file:
friend std::ostream& operator<< (std::ostream& os, linkedlist<t>& list); and implementation of 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 main function, have created list has object savingaccount class
linkedlist <savingaccount> list; and error occurs in main function in line:
cout << list <<endl; well.. 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 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 guys can me that, appreciated
ok, after posted code seems problem split linkedlist template declaration , implementation between header , source file. unless doing explicit instantiation in source file, cannot this: cannot separate template declaration , implementation, have in 1 header.
you need put content of linkedlist.cpp in linkedlist.h, rid of source file altogether, , include updated header need it.
p.s. also, modifying object through const reference not idea, speaking. compiler not it. might want reconsider design.
p.p.s. if want , mean modify data members in objects of class access via const reference, need declare data members mutable.
p.p.p.s. elaborate on template function foo friend declarations template bar class , avoid lengthy discussions in comments.
if defining such function declaration inside class, straightforward thing - use friend keyword. example, friend void foo( bar< t > & ) { ... }.
if defining separately outside class, need use different syntax in declaration , avoid shadowing of template parameters: template< typename u > friend void foo( bar< u > & ) { ... } (assuming u not used in template parameters in bar class declaration). , define outside class.
if want specify default template arguments foo gets more trickier. cannot use default template arguments in friend declaration, have forward declare everything:
template< typename t > class bar; template< typename t = int > void foo( bar< t > & ); template< typename t > class bar { template< typename u > friend void foo( bar< u > & ); }; template< typename t > void foo( bar< t > & ) { ... } and 1 last p.p.p.p.s (i hope!). since using operator << type savingaccount in operator << linkedlist, need define operator << savingaccount too.
note self : don't answer questions in comments' section answer ;).
Comments
Post a Comment