Like Tree1Likes
  • 1 Post By tabstop

Need help with an error in my linked list class

This is a discussion on Need help with an error in my linked list class within the C++ Programming forums, part of the General Programming Boards category; Edit: I fixed my initial problem, but now I am getting this linker error that I can't figure out: Hi ...

  1. #1
    Registered User
    Join Date
    Dec 2010
    Posts
    19

    Need help with an error in my linked list class

    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 found
    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;
    }
    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 by Freddy92; 09-06-2011 at 02:01 AM. Reason: Fixed my first problem, looking for help on another

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    You should never have "real" code in a header file. Templated code is okay (and in fact pretty much has to be in a header), but regular code, such as
    Code:
    ostream &operator<<(ostream & out, const Term& data)
    {
    	if (data.exponent == 0)
    		out << data.coefficient << endl;
    	else
    		out << data.coefficient << "x" << data.exponent << " + ";
    	return out;
    }
    must not appear in a header file.
    stahta01 likes this.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Linked list of a class object?....
    By chadsxe in forum C++ Programming
    Replies: 6
    Last Post: 12-08-2005, 02:15 PM
  2. Basic Linked List class
    By ExCoder01 in forum C++ Programming
    Replies: 3
    Last Post: 09-14-2003, 02:15 AM
  3. traversing a linked list with a node and list class
    By brianptodd in forum C++ Programming
    Replies: 2
    Last Post: 04-24-2003, 11:57 AM
  4. Template Class for Linked List
    By pecymanski in forum C++ Programming
    Replies: 2
    Last Post: 12-04-2001, 08:07 PM
  5. STL Linked List of class within class
    By NixPhoeni in forum C++ Programming
    Replies: 3
    Last Post: 11-30-2001, 09:17 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21