2017-08-28 137 views
-3

我想創建一個庫存鏈接列表,用戶可以在其中添加產品(id,info,price,count),然後將該對象存儲在鏈接列表中。 我遇到的問題是Node類給出的錯誤「缺少類型說明符 - int假定。注意:C++不支持default-int」等等。節點鏈接列表中的對象

#ifndef INVENTORY_H 
#define INVENTORY_H 

#include<iostream> 
#include <string>  
#include <iomanip> 
using namespace std; 

class Node 
{ 
public: 
    Node(){} 
    Node(const Inventory& theData, Node *theLink) 
     : data(theData), link(theLink){} 
    Node* getLink() const { return link; } 
    Inventory getData() const { return data; } 
    void setData(const Inventory& theData) 
    { 
     data = theData; 
    } 
    void setLink(Node *theLink) { link = theLink; } 
private: 
    Inventory data; 
    Node *link;  //pointer that points to next node 
}; 

class Inventory 
{ 
public: 
    Inventory(); 
    void addPart(int, string, int, int); 
    void findPart(int); 
    void toBinary(); 
    void quit(); 
private: 
    int id; 
    int price; 
    string partInfo; 
    int partCount; 

    Node* first; 
    Node* last; 
    int count; 
}; 
#endif 

,並且錯誤是:

1>c:\users\jeffrey\desktop\epp_fall\epp_fall\inventory.h(13): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int 
1>c:\users\jeffrey\desktop\epp_fall\epp_fall\inventory.h(13): error C2143: syntax error : missing ',' before '&' 
1>c:\users\jeffrey\desktop\epp_fall\epp_fall\inventory.h(16): error C2146: syntax error : missing ';' before identifier 'getData' 
1>c:\users\jeffrey\desktop\epp_fall\epp_fall\inventory.h(16): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int 
1>c:\users\jeffrey\desktop\epp_fall\epp_fall\inventory.h(16): warning C4183: 'getData': missing return type; assumed to be a member function returning 'int' 
1>c:\users\jeffrey\desktop\epp_fall\epp_fall\inventory.h(17): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int 
1>c:\users\jeffrey\desktop\epp_fall\epp_fall\inventory.h(17): error C2143: syntax error : missing ',' before '&' 
1>c:\users\jeffrey\desktop\epp_fall\epp_fall\inventory.h(23): error C2146: syntax error : missing ';' before identifier 'data' 
1>c:\users\jeffrey\desktop\epp_fall\epp_fall\inventory.h(23): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int 
1>c:\users\jeffrey\desktop\epp_fall\epp_fall\inventory.h(13): error C2065: 'Thedata' : undeclared identifier 
1>c:\users\jeffrey\desktop\epp_fall\epp_fall\inventory.h(13): error C2065: 'theLink' : undeclared identifier 
1>c:\users\jeffrey\desktop\epp_fall\epp_fall\inventory.h(13): error C2614: 'Node' : illegal member initialization: 'data' is not a base or member 
1>c:\users\jeffrey\desktop\epp_fall\epp_fall\inventory.h(16): error C2065: 'data' : undeclared identifier 
1>c:\users\jeffrey\desktop\epp_fall\epp_fall\inventory.h(19): error C2065: 'data' : undeclared identifier 
1>c:\users\jeffrey\desktop\epp_fall\epp_fall\inventory.h(19): error C2065: 'theData' : undeclared identifier 
+0

剛剛使用'std :: list'或'std :: vector'有什麼問題?爲什麼重新發明輪子? –

回答

1

你只是看着編譯器的標準情況,因爲你定義的順序不知道你的自定義類型是什麼。即使你轉發宣佈一個類(把「類庫存」放在上面的某個地方),你也不能將它用於某些事情。聲明一個不是指針的成員變量就是其中之一。但是,如果您反轉定義(Node之前的Inventory)並轉發declare Node(「class Node;」),則代碼將編譯,因爲您的Inventory類中只有Node *。

檢查這個答案詳細信息:https://stackoverflow.com/a/553869/128581

正如指出的那樣,如果這個代碼是沒有嚴格的學習,這是不是最好的設計。 STL容器覆蓋最常見的數據結構(雙鏈接和單鏈表是其中的兩個)。 stl :: list和stl :: forward_list。

0

C++編譯器讀取源代碼從上到下。當它到達第13行時,它還沒有聽說過Inventory類型。它認爲Inventory因此必須是參數名稱,並且變得非常困惑。

對此的解決方案是切換NodeInventory類的順序,和Inventory類開始前,由前所述插入以下行

class Node; 

預先聲明的Node類開始class Inventory

切換訂單InventoryNode之前定義,因爲編譯器需要了解的一切Inventory構建Node,因爲Node小號包含Inventory S的是很重要的。額外的一行告訴編譯器,有一個Node類,但not anything about what it is;這樣你可以使用指向Node的指針(或者其他不需要知道Node的佈局的指針),但是在完全定義之前不能對它們做任何其他操作。由於Inventory只使用指針,所以這應該不成問題。


然而,如上所述我不明白爲什麼Inventory需要在所有節點的指針;它看起來像你的Inventory類是你所說的英文說明中的產品,而產品不需要知道其餘的數據。您也可能只想使用std::forward_list而不是嘗試實現自己的鏈接列表類型。