2012-02-22 32 views
-5

我想創建一個鏈接列表的程序。我想創建一個case語句,用戶可以選擇 1.添加節點 2.刪除節點 3.搜索節點 4.顯示鏈表(每行5個節點) 5.退出程序創建鏈接列表 - 編譯器錯誤

但是該程序仍在進行中,我無法將其編譯到目前爲止。我試圖找出是什麼導致它不能編譯。看來我的主程序不會鏈接頭文件。任何建議,而我不能編譯或鏈接?我想在繼續工作之前弄清楚出錯的原因。另外哪裏是創建我的結構最好的地方?

從流血開發的C++

list.cpp: In constructor list::list(): 
list.cpp:7: error: node undeclared (first use this function) 
list.cpp:7: error: (Each undeclared identifier is reported only once for each function it appears in.) 
list.cpp:7: error: head undeclared (first use this function) 
list.cpp:8: error: precurrent undeclared (first use this function) 
list.cpp:9: error: current undeclared (first use this function) 

list.cpp: At global scope: 
list.cpp:16: error: expected constructor, destructor, or type conversion before :: token 
list.cpp:16: error: expected , or ; before :: token 
list.cpp: In member function void list::insert(int): 
list.cpp:24: error: head undeclared (first use this function) 
list.cpp:24: error: null undeclared (first use this function) 
list.cpp:24: error: stray \ in program 
list.cpp:24: error: stray \ in program 
list.cpp:24: error: If undeclared (first use this function) 

Main.cpp的

錯誤

#include <string> 
#include <iostream> 
using namespace std; 
#include <cstdlib> 
#include "list.h" 

using namespace std; 

int main(int argc, char *argv[]) 
{ 
list* list1; 



system("PAUSE"); 
return EXIT_SUCCESS; 
} 

Link.cpp

#include <iostream> 
using namespace std; 
#include "list.h" 

list::list() 
{ 
node *head = NULL; 
node *precurrent = NULL; 
node *current = NULL; 
int *temp = 0; 
int _insert = 0; 
int _search = 0; 
int _remove = 0; 
} 

List::~list() 
{ 
while (head != 0) 
    remove(); 
} 

void list::insert(int _insert) 
{ 
if (head==null) \\If there is no list already, create a new head. 
{ 
    temp = new Node; 
    temp->data = _insert; 
    head = temp; 
} 
else    \\otherwise, insert the new node after current 
{ 
    temp = new Node; 
    temp->data = _insert; 
    temp->next = current->next; 
    current->next = temp; 
} 
} 

void list::search(int _search) 
{ 
current=head; 
while (head->next != 0) //Cycle through the list, and if the number is found, say so 
{ 
    if (current->data = _search) 
     cout<<"Number found."<<endl; 
    else 
     cout<<"Number not found."<<endl; 
} 
} 

void list::remove(int _remove) 
{ 
if (head == null) 
    cout <<"Error. No list."<<endl; 
else if (head->next == null) 
{ 
    num = head->data; 
    delete head; 
    head=null; 
    current=null; 
} 
else if (head == current) 
{ 
    temp = head->next; 
    num = head->data; 
    delete head; 
    head=temp; 
    current=temp; 
} 
else 
{ 
    temp = current->next; 
    num = current->data; 
    delete current; 
    precurrent->next = temp; 
    current = temp; 
} 
} 

ostream &operator<<(ostream& osObject, list& list){ 

nodeType<Type>* current = list.head; 
int i = 0; 

while (current != NULL) //while more data to print 
{ 
osObject << current->info << " "; 
current = current->link; 

++i; 

if (i % 5 == 0) 
{ 
     cout << '\n'; 
     i = 0; 
} 
} 

osObject << '\n'; // print the ending newline 

    return osObject; 
} 

Link.h

//CLASS PROVIDED: list         
// 
// CONSTRUCTOR for the list class: 
// list() 
//  Description:  Constructor will initialize variables 
//  Preconditions: None 
//  Postcondition: int insert = "" 
//      int search = "" 
//      int remove = "" 
// ~list() 
//  Description:  Destructor destroys variables 
//  Preconditions: None 
//  Postcondition: variable deleted 
// 
// MEMBER FUNCTIONS for the list class  
// 
// string insert(int) 
//  Description: Inserts an integer into a linked list 
    //  Precondition: none 
    //  Postcondition: function returns Success/Error message. 
    // 
    // string search(int); 
    //  Description:  Searches for certain linked list member and returns int to set current variable 
    //  Precondition: none 
    //  Postcondition: function returns int 
    // 
    // string remove(int); 
    //  Description:  removes linked list member 
    //  Precondition: user sends int to be deleted 
    //  Postcondition: function returned string sddress 
    // 
    // void display(void); 
    //  Description:  displays entire linked list 
    //  Precondition: none 
    //  Postcondition: function returns screen output 
    // 
    // void quit(void); 
    //  Description:  closes program 
    //  Precondition: none 
    //  Postcondition: none 
    // 


    #ifndef LIST_H 
    #define LIST_H 

    #include <string> 
    #include <iostream> 
    #include <cstdlib> 

    using namespace std; 

    class list 
    {  
    friend ostream& operator<< (ostream &os, const list&); 
    public: 
    //CONSTRUCTOR/DESTRUCTOR--------------------- 
    list(); 
    ~list();          

    //GETS--------------------------------------- 
    void insert(int); 
    string search(int); 
    string remove(int); 
    void display(void); 
    void quit(void); 

    private: 

     int insert; 
     int search; 
     int remove; 


     }; 




     #endif 
+0

什麼是'#inclide'? – Drahakar 2012-02-22 05:57:47

+0

我們需要你有編譯錯誤。 – Drahakar 2012-02-22 05:59:18

+2

而傳統的方法是輸入幾行代碼,編譯它們(並糾正它們直到它們編譯),然後重複。 – 2012-02-22 06:04:42

回答

0

它的抱怨是因爲幾個方法和變量在你的類中有相同的名字。即,插入,搜索和刪除。

考慮用'_'開始變量名稱。

+0

已更新的變量名稱和錯誤 – dtturner12 2012-02-22 06:32:50

+0

在您的實現文件中,您的標題和大寫('List')中的'list'是小寫字母。 – 2012-02-22 06:36:06

2

請在提問前仔細閱讀錯誤。這非常重要,因爲可以使用等待某人回覆的時間來找出代碼中最明顯的問題。

根據新的錯誤,

list.cpp:24: error: stray '\' in program 
list.cpp:24: error: stray '\' in program 

難道你懶得讀上面我的意見嗎?檢查一下。你可以看到這真的很明顯,爲什麼。

list.cpp:5: error: `List' has not been declared 

這是什麼意思?看看你的.h文件和你的List.cpp文件。不同的是,.h中的類是小寫list,而在cpp中,您的大寫字母爲List。讓你的班級名字首字母大寫。這是一個慣例。

list.cpp:7: error: `node' undeclared (first use this function) 
list.cpp:7: error: `head' undeclared (first use this function) 
    ... and the rest 

否否否您對整個班級設計有問題。 .h文件應該是數據抽象,並且您的List.cpp包含實現或控件抽象。

用簡單的英語,在.h文件,你需要speifcy的數據結構(你打算爲這個鏈表使用什麼樣的類型,有哪些成員變量?),然後在.cpp你寫的邏輯類操作(刪除,插入等)。您的node類型也不存在。所以最終你沒有head,current等,但即使你有node類實現,這也是錯誤的。因爲您應該在您的.h文件中列出您的數據成員(節點類型,int等),然後在Link.cpp中編寫控制器,析構函數和操作。


此外,您的私人成員不一定只是變量。有時候你可以讓你的某個功能變成私人的。如果你想讓你的remove()是私人的(公衆用戶無法訪問),你可以。

但是回到話題:我甚至都沒有看到讓你擁有這些數據成員的重要性。

我會給你是這兒的大提示:結構應該像這樣

List.h 
    //here is the datatype for nodes of the list: 
    struct listNode 
    { 
     //constructors: 
     listNode(); 
     listNode(int x, listNode* pNext = 0); 

     //data members: 
     listNode* next; //pointer to the next node in the list 
     int data; //the data for the node, make it simple we use int 
    }; 
     class List 
     { 
      public: 
       List(); 
       // In this line, you probably want to have a constructor takes in a list 
       List(const List&L); // also known as COPY CONSTRUCTOR 
       ~List(); 


       // your member functions here 

      private: 
       listNode* root;  // right, a List should have a root, right? 
     } 

這是我的實現。但是你應該一目瞭然,並且找出每位教授指導員自己的實現。

+1

我想我最終會搗毀這個程序並重新開始。似乎解決這一問題的工作量幾乎等於 – dtturner12 2012-02-22 07:06:47

+0

謝謝你的所有幫助 – dtturner12 2012-02-22 07:14:43

+0

請不要將用戶指向Google。這是一個搜索引擎,而不是知識庫。 – Mast 2015-04-16 19:13:17