2012-11-28 73 views
0

我正在嘗試將客戶存儲在鏈接列表中。我是C++的新手,所以我嘗試將一個int鏈表修改爲Customer鏈表。這是我的客戶和列表代碼以及它的主要運行。我得到的錯誤是:未插入數據的對象鏈接列表

1> C:\用戶\ tashiemoto \桌面\ C++ \ assignmentfinal \ assignmentfinal \ main4.cpp(12):錯誤C2182: 'addToList':非法使用類型爲 '無效' 1> C:\用戶\ tashiemoto \桌面\ C++ \ assignmentfinal \ assignmentfinal \ main4.cpp(12):錯誤C2440:初始化:不能從 '顧客*' 到 'INT'

轉換哪個當我嘗試向main.cpp上的鏈接列表中添加一個新的數據條目時發生。我總是在main的addtoList下有一條紅線。我認爲這與客戶有關,但我不確定。

Main.cpp的

#include <iostream> 
#include "customer.h" 
#include "list.h" 

void main() 

{ 
    //construction 
    LinkedList(); 

    //add a new node to the last 
    void addToList(new Customer("hhht","hhh","hhh","hhhkjk","klio")); 

} 

list.h

#include "customer.h" 

//forward declaration 
class Node; 

//class definition 
class LinkedList 
{ 
public: 
    //construction 
    LinkedList(); 

    //add a new node to the last 
    void addToList(Customer *data); 

    //find an element in list and set current pointer 
    void find(int key); 

    //get data from element pointed at by current pointer 
    Customer* getCurrent(void); 

    //delete element pointed at by current pointer 
    void deleteCurrent(void); 

private: 
    //data members 
    Node *_begin;  //pointer to first element in list 
    Node *_end;  //pointer to last element in list 
    Node *_current; //pointer to current element in list 
}; 

list.cpp

LinkedList::LinkedList() 
{ 
    //initialise node pointers 
    _begin = NULL; 
    _end = NULL; 
    _current = NULL; 
} 

void LinkedList::addToList(Customer *data) 
{ 
    Node *newNodePtr; 

    //craete new instance of Node 
    newNodePtr = new Node; 

    //set data 
    newNodePtr->setData(data); 

    //check if list is empty 
    if(_begin == NULL) 
    { 
     _begin = newNodePtr; 
    } 
    else 
    { 
     newNodePtr->setPrevNode(_end); 
     _end->setNextNode(newNodePtr); 
    } 

    //set current pointer end end pointer 
    _end = newNodePtr; 
    _current = newNodePtr; 
} 

Customer.h

​​3210

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

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

沒有人有時間去通過這個牆的代碼!。 請只發布相關的代碼。 –

+0

你應該得到一本好書http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list – Caribou

+0

其實......發生了什麼事http://stackoverflow.com/questions/13597327/c-linked-list-with-objects-that-is-coming-up-with-the-lnk2019-error – Caribou

回答

0

的問題顯得相對容易地址:

void main() 

{ 
    // This is wrong, it constructs an unnamed temporary. 
    //construction 
    // LinkedList(); 

    // This is how you default construct a LinkedList item 
    LinkedList myList; 

    // This is wrong, you don't use the full signaure when calling a function 
    //add a new node to the last 
    // void addToList(new Customer("hhht","hhh","hhh","hhhkjk","klio")); 

    // This calls the addToList member function of the myList object 
    myList.addToList(new Customer(...)); 

} 
0
void main() 

{ 
    //construction 

    //LinkedList(); <-- This is not valid 

    LinkedList myList; 


    //add a new node to the last 

    // void addToList(new Customer("hhht","hhh","hhh","hhhkjk","klio")); <-- This 
    //                is not valid 

    myList.addToList(new Customer("hhht","hhh","hhh","hhhkjk","klio")); 

} 
0

好了,讓雅滾動這些錯誤,申報的LinkedList的實例:

LinkedList myList; 

,並調用函數:

myList.addToList(new Customer("hhht","hhh","hhh","hhhkjk","klio")); 

但是你似乎對C++最基本的元素是編寫自己的鏈表感到困惑 - 而且無論如何,STL提供了一個。