节点链接列表中的对象

时间:2017-08-28 21:33:39

标签: c++ linked-list

我正在尝试创建一个库存链表,用户可以在其中添加产品(ID,信息,价格,计数),然后将对象存储在链表中。 我遇到的问题是Node类给出错误“缺少类型说明符 - 假设为int。注意:C ++不支持default-int”等等,对于Node类中的每个语句都是如此。

#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

2 个答案:

答案 0 :(得分:1)

您只是在查看编译器的标准情况,因为您已定义它们的顺序,因此不知道您的自定义类型是什么。即使你转发声明一个类(把“class Inventory;”放在上面的某个地方),你也不能将它用于某些事情。声明不是指针的成员变量就是其中之一。

但是,如果您反转定义(节点前的清单)和转发声明节点(“类节点;”),您的代码将编译,因为您的库存类中只有Node *。

查看此答案以获取更多详细信息:https://stackoverflow.com/a/553869/128581

正如所指出的,如果此代码不是严格用于学习,那么这不是最好的设计。 STL容器涵盖了大多数常见的数据结构(双向链接和单链接列表是其中两个)。分别为stl :: list和stl :: forward_list。

答案 1 :(得分:0)

C ++编译器从上到下读取源代码。当它到达第13行时,它没有听说过Inventory类型。因此,它认为Inventory必须是参数名称,并且真的很困惑。

解决方法是切换NodeInventory类的顺序,并在Node类开始之前预先声明Inventory类,通过插入以下行

class Node;

class Inventory开始之前。

Inventory之前定义Node切换顺序,这很重要,因为编译器需要知道Inventory的所有内容,以构建Node,因为{{1}包含Node s。额外的行告诉编译器有一个Inventory类,但是not anything about what it is;通过这种方式,您可以使用指向Node的指针(或其他任何不需要知道Node布局的指针),但在完全定义之前不能对它们执行任何其他操作。由于Node仅使用指针,因此这应该不是问题。

但是,如上所述,我不明白为什么Inventory需要节点指针;您的Inventory类似乎就是您在英语描述中所称的产品,而产品不需要知道其余数据。您也可能只想使用std::forward_list而不是尝试实现自己的链接列表类型。

相关问题