Need help with an error in my linked list class

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:

Edit: The initial error is fixed, but now I'm getting a linker error that I can't figure out. Here is the error and the parts of code relating to it:

1
2
3
4
5
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 found


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#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;
}


It 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?
Last edited on
implement ListNode default constructor and be happy
Ah, I see now. I had tried to add a default constructor to ListNode on line 24, but all that did was generate a new error so I removed it. However I just put it back in, and then fixed the error that it caused, so now this isn't my problem anymore. Thanks. Of course, now I have a new problem that I get to figure out! Oh boy.
Topic archived. No new replies allowed.