C ++带有LNK2019错误的对象的链接列表 [英] C++ Linked list with Objects that is coming up with the LNK2019 error

查看:123
本文介绍了C ++带有LNK2019错误的对象的链接列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Basicaly这是我的分配项目的C + +和我是新的C + +,我得到这个错误。也有一个错误的注释代码在主==不是匹配的操作数

Basicaly this is my assignment project for C++ and i'm pretty new to C++ and i'm getting this error up. Also having an error for the commented code in main == is not a matched operand

1> Main2.obj:error LNK2019:unresolved external symbolclass std :: basic_ostream >& __cdecl operator<<(class std :: basic_ostream&,class Customer&)(?? 6 @ YAAAV?$ basic_ostream @ DU?$ char_traits @ D @ std @@@ std @AAV01 @ AAFCustomer @@@ Z)在函数void __cdecl display(struct Node *)(?display @@ YAXPAUNode @@@ Z)

1>Main2.obj : error LNK2019: unresolved external symbol "class std::basic_ostream > & __cdecl operator<<(class std::basic_ostream > &,class Customer &)" (??6@YAAAV?$basic_ostream@DU?$char_traits@D@std@@@std@@AAV01@AAVCustomer@@@Z) referenced in function "void __cdecl display(struct Node *)" (?display@@YAXPAUNode@@@Z)

.cpp

    #include <iostream>
    #include "account.h"
    #include "cheque.h"
    #include "customer.h"


    using namespace std;

    struct Node {
    Node* m_next;
    Customer& m_customer;
    Node(Customer& customer) : m_next(0), m_customer(customer) {}   
    };

   // only for the 1st Node
    void initNode(struct Node *head,Customer n){
head->m_customer = n;
head->m_next =NULL;
    }

     // apending
    void addNode(struct Node *head, Customer n) {
Node *newNode = new Node(n);
newNode->m_customer = n;
newNode->m_next = NULL;

Node *cur = head;
while(cur) {
    if(cur->m_next == NULL) {
        cur->m_next = newNode;
        return;
    }
    cur = cur->m_next;
}
    }


    void insertFront(struct Node **head, Customer n) {
Node *newNode = new Node(n);
newNode->m_customer = n;
newNode->m_next = *head;
*head = newNode;
    }

    //struct Node *searchNode(struct Node *head, Customer n) {
//Node *cur = head;
//while(cur) {
    //if(cur->m_customer == n) return cur;
    //cur = cur->m_next;
//}
//cout << "No Node " << n << " in list.\n";
    //}

    bool deleteNode(struct Node **head, Node *ptrDel) {
Node *cur = *head;
if(ptrDel == *head) {
    *head = cur->m_next;
    delete ptrDel;
    return true;
}

while(cur) {
    if(cur->m_next == ptrDel) {
        cur->m_next = ptrDel->m_next;
        delete ptrDel;
        return true;
    }
    cur = cur->m_next;
}
return false;
     }

     void deleteLinkedList(struct Node **node)
    {
struct Node *tmpNode;
while(*node) {
    tmpNode = *node;
    *node = tmpNode->m_next;
    delete tmpNode;
}
    }

    void display(struct Node *head) {
Node *list = head;
while(list) {
    cout << list->m_customer << " ";
    list = list->m_next;
}
cout << endl;
cout << endl;
     }

    int main() 
     {
     Customer test1("sdfs","sdf2","dsfpppsf","fdgdfg","fdgdsffg");

struct Node *newHead;
struct Node *head = new Node(test1);


Customer test2("sdsffs","sdfhhmj2","dsfhfsf","fdgdfgs","fdsggdfg");
Customer test3("sdsdfs","sdllllf2","dsfldfgsf","fdgaghdfg","fdgbvcbdfg");
Customer test4("sdgnfgfs","ssdfsdf2","dsfhjhdsf","fdbvcgdfg","fdgsfddfg");

addNode(head,test2);
display(head);

addNode(head,test3);
display(head);

insertFront(&head,test4);
display(head);

cout << "Deleting the copied list\n";
deleteLinkedList(&newHead);
display(newHead);
return 0;
     }

Customer.cpp

Customer.cpp

    #include "customer.h"

    using namespace std;


     Customer::Customer(string init_name, string init_address, string init_telephone, string init_sex, string init_dob)
        {
      name = init_name;
address = init_address;
telephone = init_telephone;
sex = init_sex;
dob = init_dob;
     }


           void Customer::showPersonDetails(void)
           {
        cout << "Name : " 
        << name << endl;
        cout << "Address : " 
        << address << endl ;
        cout << "Telephone : " 
        << telephone << endl;
        cout << "Sex : " 
        << sex << endl ;
        cout << "Date of Birth : " 
        << dob << endl;
           }

最后是customer.h

and finally customer.h

  #ifndef CUSTOMER_H
      #define CUSTOMER_H

  #include <iostream>
  #include<string>

 using namespace std;

  class Customer
   {
   private:
     //
     // class members
     //
     string name; 
     string address;
     string telephone;   
     string sex;
     string dob;

   public:

     // Constructor
     Customer(string init_name, string init_address, string init_telephone, string init_sex, string init_dob);   

     // a print method
     void showPersonDetails(void);
     void changeDetails(void);

     // This operator is a friend of the class, but is NOT a member of the // class:
     friend ostream& operator <<(ostream& s, Customer& a);

        };  

        // This is the prototype for the overload
       ostream& operator <<(ostream& s, Customer& a);

           #endif


推荐答案

您的标题有以下声明:

// This operator is a friend of the class, but is NOT a member of the class:
friend ostream& operator <<(ostream& s, Customer& a);

您的cpp文件中没有定义 - 您需要提供定义。 p>

You don't have a definition in your cpp file - you need to provide a definition.

ostream& operator <<(ostream& s, Customer& a)
{
    s << a.name << ... etc...
    return s;
}

出现链接器错误的原因

Main.cpp 中有

In Main.cpp you have the display function which uses operator<< for Customer:

void display(struct Node *head) {
Node *list = head;
while(list) {
    cout << list->m_customer << " "; // this line here uses operator<< for Customer
    list = list->m_next;
}

编译,因为在头部中存在运算符<< ,因此可以找到符号...但是在链接阶段中,对象定义无法找到运算符< <) - 因此您会得到一个未解决的外部链接器错误。

It compiles because operator<< exists in the header, so the symbol can be found... but in the linking stage the object definition (the body of operator<<) cannot be found - so you get an unresolved external linker error.

这篇关于C ++带有LNK2019错误的对象的链接列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆