简体   繁体   English

C ++ <<使用ostream重载以获得正确的格式

[英]C++ << overloading with ostream for correct formatting

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. 我在这些类中遇到以下错误,但Visual Studio 2010没有给出行号。 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 .cpp file instead. 您需要内联定义,或者更好的是,将其放在.cpp文件中。 In Customer.h just put a function prototype: Customer.h只需输入一个函数原型:

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

And put the full definition in Customer.cpp : 并将完整定义放在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关键字。 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)
{
    ...
}

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 . 如果函数是函数模板或已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. 通常的过程是在头文件中只放置一个声明,并将定义放在与类成员函数定义相同的源文件中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM