节点链表中的对象
Object in Node Linked List
我正在尝试创建一个库存链表,用户可以在其中添加产品(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
一定是参数名称,并感到非常困惑。
解决方案是切换Node
和Inventory
类的顺序,并在Inventory
类开始之前预先声明Node
类,方法是插入以下行
class Node;
在CCD_ 7开始之前。
切换顺序在Node
之前定义了Inventory
,这一点很重要,因为编译器需要了解Inventory
的所有信息才能构建Node
,因为Node
s包含Inventory
s。额外的一行告诉编译器有Node
类,但不知道它是什么;通过这种方式,您可以使用指向Node
的指针(或任何不需要了解Node
布局的其他指针(,但在完全定义它们之前,不能对它们执行任何其他操作。由于Inventory
只使用指针,所以这应该不是问题。
然而,如上所述,我根本不明白Inventory
为什么需要节点指针;你的Inventory
类似乎就是你在英语描述中所说的产品,而产品不需要知道其余的数据。您可能还想只使用std::forward_list
,而不是尝试实现自己的链表类型。
- 为什么不能修改对象中的值?另外,我如何改进此链表?
- C++ 创建包含链表和字符串的对象的链接列表时出错
- 链表包含 c++ 中不同的派生类对象
- 如何深度复制链表对象指针
- 如何通过不同的指针使用类的对象访问结构?(链表)(C++)
- 节点链表中的对象
- 按引用或值传递链表对象更好吗?
- 基于范围的 for 循环,用于包含C++中的指针的自定义链表,仅返回对象
- C++ 如何创建链表的副本作为类对象
- 无法分配给链表中的对象
- C++ 链表:"->"运算符无法更改节点对象的内部值
- 具有自己的链表的对象
- 存储类对象的C++模板链表-未处理的异常访问冲突读取位置
- 从链表访问对象(自定义实现)
- 创建面向对象的链表时出现编译问题(编译器错误C2664)
- 对象数组作为链表中的字符数组 - 我对对齐的假设是否有效
- 将对象添加到链表错误 [C++]
- C++ 链表:对象的索引
- 如何将链表对象写入文件
- 如何在C++中将对象转换为链表