我已经开始编写自己的链接列表,它可以很好地打印数字,但我使用模板来确定用于对象的类型名。因此,除了打印对象外,我输入数据没有任何问题。这些类出现以下错误,但 Visual Studio 2010 没有给出行号。我要做的就是允许从链接列表中以正确的格式输出不同类型的对象。
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
桂班
//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;
}
客户类
//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;
}