I’ve started to play with coding my own linklist, It works fine for printing numbers but i use Templates in order to determine typename for use with objects. As such i have no issues entering data except printing objects. I get the following error with these classes but Visual studio 2010 doesn’t give a line number. All i’m trying to do is allow different types of objects to be outputted from the linklist with the correct formatting.
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 Customer &)" (??6@YAAAV?$basic_ostream@DU?$char_traits@D@std@@@std@@AAV01@AAVCustomer@@@Z)
already defined in Customer.obj
Gui Class
//Templates
#include "LinkList.h"
#include "Node.h"
//Classes
#include "Gui.h"
#include "Customer.h"
//Libaries
#include <iostream>
//Namespaces
using namespace std;
int main(){
Customer c1("TempFirst", "TempLast");
LinkList<Customer> customerList;
customerList.insert(c1);
//Print Linklist
customerList.print();
system("pause");
return 0;
}
Customer Class
//Def
#pragma once
//Included Libaries
#include <string>
#include <iostream>
class Customer
{
private:
std::string firstName;
std::string lastName;
public:
Customer();
Customer(std::string sFirstName, std::string sLastName);
~Customer(void);
//Get Methods
std::string getFirstName();
std::string getLastName();
//Set Methods
void setFirstName(std::string sFirstname);
void setLastName(std::string sLastname);
//Print
};
std::ostream& operator << (std::ostream& output, Customer& customer)
{
output << "First Name: " << customer.getFirstName() << " "
<< "Last Name: " << customer.getLastName() << std::endl;
return output;
}
Careful about putting function definitions in the header file. You either need to inline the definition or, better yet, put it in a
.cppfile instead. InCustomer.hjust put a function prototype:And put the full definition in
Customer.cpp:Alternatively, if you really want the definition in the header file then add the
inlinekeyword. Inline definitions have to go in header files, and by their nature they don’t have external linkage and won’t trigger duplicate definition errors.