Question

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;
}
Was it helpful?

Solution

Careful about putting function definitions in the header file. You either need to inline the definition or, better yet, put it in a .cpp file instead. In Customer.h just put a function prototype:

// Customer.h
std::ostream& operator << (std::ostream& output, Customer& customer);

And put the full definition in Customer.cpp:

// Customer.cpp
std::ostream& operator << (std::ostream& output, Customer& customer)
{
    output << "First Name: " << customer.getFirstName() << " "
           << "Last Name: "  << customer.getLastName()  << std::endl;
    return output;
}

Alternatively, if you really want the definition in the header file then add the inline keyword. 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.

inline std::ostream& operator << (std::ostream& output, Customer& customer)
{
    ...
}

OTHER TIPS

Your not getting a line number, because the error isn't detected until link time, and the linker doesn't see any source code. The problem is that you've put the function definition in the header; this is only legal if the function is a function template or has been declared inline. The usual procedure is to put just a declaration in the header, and put the definition in the same source file as the class member function definitions.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top