2017-08-28 118 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而不是尝试实现自己的链接列表类型。