节点链表中的对象

Object in Node Linked List

本文关键字:对象 链表 节点      更新时间:2023-10-16

我正在尝试创建一个库存链表,用户可以在其中添加产品(id、info、price、count(,然后将对象存储在链表中。我遇到的问题是Node类给出错误"缺少类型说明符-假定为int。注意:C++不支持默认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:usersjeffreydesktopepp_fallepp_fallinventory.h(13): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:usersjeffreydesktopepp_fallepp_fallinventory.h(13): error C2143: syntax error : missing ',' before '&'
1>c:usersjeffreydesktopepp_fallepp_fallinventory.h(16): error C2146: syntax error : missing ';' before identifier 'getData'
1>c:usersjeffreydesktopepp_fallepp_fallinventory.h(16): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:usersjeffreydesktopepp_fallepp_fallinventory.h(16): warning C4183: 'getData': missing return type; assumed to be a member function returning 'int'
1>c:usersjeffreydesktopepp_fallepp_fallinventory.h(17): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:usersjeffreydesktopepp_fallepp_fallinventory.h(17): error C2143: syntax error : missing ',' before '&'
1>c:usersjeffreydesktopepp_fallepp_fallinventory.h(23): error C2146: syntax error : missing ';' before identifier 'data'
1>c:usersjeffreydesktopepp_fallepp_fallinventory.h(23): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:usersjeffreydesktopepp_fallepp_fallinventory.h(13): error C2065: 'Thedata' : undeclared identifier
1>c:usersjeffreydesktopepp_fallepp_fallinventory.h(13): error C2065: 'theLink' : undeclared identifier
1>c:usersjeffreydesktopepp_fallepp_fallinventory.h(13): error C2614: 'Node' : illegal member initialization: 'data' is not a base or member
1>c:usersjeffreydesktopepp_fallepp_fallinventory.h(16): error C2065: 'data' : undeclared identifier
1>c:usersjeffreydesktopepp_fallepp_fallinventory.h(19): error C2065: 'data' : undeclared identifier
1>c:usersjeffreydesktopepp_fallepp_fallinventory.h(19): error C2065: 'theData' : undeclared identifier

您所看到的只是编译器不知道自定义类型是什么的标准情况,因为您定义了它们的顺序。即使您正向声明一个类(将"classInventory;"放在上面的某个位置(,也不能将其用于某些事情。声明不是指针的成员变量就是其中之一。

但是,如果反转定义(Inventory before Node(并正向声明Node("class Node;"(,则代码将编译,因为Inventory类中只有Node*。

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

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

C++编译器从上到下读取源代码。当它到达第13行时,它还没有听说过Inventory类型。因此,它认为Inventory一定是参数名称,并感到非常困惑。

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

class Node;

在CCD_ 7开始之前。

切换顺序在Node之前定义了Inventory,这一点很重要,因为编译器需要了解Inventory的所有信息才能构建Node,因为Nodes包含Inventorys。额外的一行告诉编译器有Node类,但不知道它是什么;通过这种方式,您可以使用指向Node的指针(或任何不需要了解Node布局的其他指针(,但在完全定义它们之前,不能对它们执行任何其他操作。由于Inventory只使用指针,所以这应该不是问题。


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