【问题标题】:C++ Linked list with Objects that is coming up with the LNK2019 error带有 LNK2019 错误的对象的 C++ 链表
【发布时间】:2012-11-15 20:19:36
【问题描述】:

基本上这是我的 C++ 作业项目,我对 C++ 很陌生,我遇到了这个错误。 main == 中的注释代码也有错误不是匹配的操作数

1>Main2.obj : 错误 LNK2019: 无法解析的外部符号“class std::basic_ostream > & __cdecl operator &,class Customer &)” (??6@YAAAV?$basic_ostream @DU?$char_traits@D@std@@@std@@AAV01@AAVCustomer@@@Z) 在函数“void __cdecl display(struct Node *)”中引用 (?display@@YAXPAUNode@@@Z)

这里是代码 Main.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;
     }

客户.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

  #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

【问题讨论】:

  • 我已经暗示了下面的答案,现在在 AssignmentFinal.exe 中的 0x57d01f68 (msvcp100d.dll) 处得到未处理的异常:0xC0000005:访问冲突读取位置 0xcccccccc。 which 指向静态 int_type __CLRCALL_OR_CDECL to_int_type(const _Elem& _Ch) { // 将字符转换为元字符 return ((unsigned char)_Ch); }
  • 这是一个不同的问题,因此您应该创建一个新问题。然而,话虽如此,在粗略地查看您的代码之后,这看起来很可疑:您在用newHead 做什么?在调用 deleteLinkedList(&amp;newHead); 之前,您似乎没有将其初始化为任何内容

标签: c++ object linked-list lnk2019


【解决方案1】:

在您的标题中,您有以下声明:

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

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

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

您遇到 linker 错误的原因是声明存在,但定义不存在。

Main.cpp 中,您有display 函数,它使用operator&lt;&lt; 表示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;
}

编译因为operator&lt;&lt;存在于标头中,所以可以找到符号...但是在链接阶段对象定义( operator&lt;&lt;) 的主体 无法找到 - 因此您会收到 unresolved external 链接器错误。

【讨论】:

  • 我已经隐含了运算符,现在在 AssignmentFinal.exe 中的 0x57d01f68 (msvcp100d.dll) 处得到未处理的异常:0xC0000005:访问冲突读取位置 0xcccccccc。 which 指向静态 int_type __CLRCALL_OR_CDECL to_int_type(const _Elem& _Ch) { // 将字符转换为元字符 return ((unsigned char)_Ch);这是什么意思:S
【解决方案2】:

您应该在 Customer.cpp 中为您的 Customer 类实现 operator&lt;&lt;

friend ostream& operator <<(ostream& s, Customer& a)
{
        s << "Name : "         << a.name << endl;
        s << "Address : "      << a.address << endl ;
        s << "Telephone : "    << a.telephone << endl;
        s << "Sex : "          << a.sex << endl ;
        s << "Date of Birth : "<< a.dob << endl;
        return s;
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-30
    • 2015-06-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多