2012-11-28 81 views
1

Basicaly這是我的C++任務項目,我很新的C++,我得到這個錯誤。還具有用於在主==註釋的代碼的錯誤是不是一個匹配操作數C++與LNK2019錯誤的對象鏈接列表

1> Main2.obj:錯誤LNK2019:解析外部符號「類的std :: basic_ostream> & __cdecl操作者< <(類的std :: basic_ostream> &,class Customer &)「(?? 6?YAAAV?$ basic_ostream @ DU?$ char_traits @ D @ std @@@ std @@ AAV01 @ AAVCustomer @@@ Z)在函數」void __cdecl display(struct節點*)」(?顯示@@ 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; 
    } 

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

#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 
+0

我在AssignmentFinal.exe implimented下面的答案,現在0x57d01f68(msvcp100d.dll)得到未處理的異常:0000005:訪問衝突讀取位置0xcccccccc。它指向\t靜態INT_TYPE __CLRCALL_OR_CDECL to_int_type(常量_Elem&_CH) \t \t \t {//轉換字符到元字符 \t \t回報((無符號字符)_CH); \t \t} – Learning

+0

這是一個不同的問題,因此您應該創建一個新的問題。然而,那就是說,在對你的代碼進行表面審視之後,這看起來很可疑:你在用newHead做什麼?在調用'deleteLinkedList(&newHead);' –

回答

2

在你的頭,你有以下聲明:

// 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; 
} 

你有一個連接錯誤的原因是因爲存在申報,而不是定義。

Main.cpp你有使用display功能operator<<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<<,所以符號可以找到... ...但在連接階段的對象定義(正文operator<<)無法找到 - 所以你得到unresolved external鏈接器錯誤。

+0

之前,您看起來並不像初始化它任何東西我已經實現了操作符,現在AssignmentFinal.exe中的0x57d01f68(msvcp100d.dll)得到未處理的異常:0xC0000005:訪問衝突讀取位置0xcccccccc 。它指向靜態int_type __CLRCALL_OR_CDECL to_int_type(const _Elem&_Ch){//將字符轉換爲元字符返回((unsigned char)_Ch); }這是什麼意思:S – Learning

0

你應該實現operator<<爲您的客戶類customer.cpp中

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; 
}