Edit: I fixed my initial problem, but now I am getting this linker error that I can't figure out:
Hi guys, I'm trying to make a template linked list class, but I keep getting this error when I try to run the linked list class:
Code:error LNK2005: "class std::basic_ostream<char,struct std::char_traits<char> > & __cdecl operator<<(class std::basic_ostream<char,struct std::char_traits<char> > &,class Term const &)" (??6@YAAAV?$basic_ostream@DU?$char_traits@D@std@@@std@@AAV01@ABVTerm@@@Z) already defined in poly.obj 1>C: ....... : fatal error LNK1169: one or more multiply defined symbols foundIt seems that the error is occurring because I defined two different operator << functions, but I tried commenting one out and that didn't help, so I can't figure out how to fix this one. Can anyone help me out?Code:#include <iostream> using namespace std; #ifndef LISTP_H #define LISTP_H template<typename T> class List { friend ostream &operator<< <T>(ostream &, const List<T> &); . . . }; // Further down in the file template< typename T> ostream &operator<<(ostream &out, const List<T> &aList) { T temp; for (i = 1; i <= size; i++) { aList.retrieve(i, temp); out << temp; } return out; } //In another class file #include "llist.h" class Term { friend ostream &operator<<(ostream &, const Term&); . . . }; ostream &operator<<(ostream & out, const Term& data) { if (data.exponent == 0) out << data.coefficient << endl; else out << data.coefficient << "x" << data.exponent << " + "; return out; } //Finally, main #include <iostream> #include "poly.h" using namespace std; int main() { List<Term> ll; Term x(5, 11), y(12, 5), z(8, 0); if (x == 11) cout << x << y << z << endl; ll.insert(1, x); return 0; }



1Likes
LinkBack URL
About LinkBacks


