制作链表向量?
如何制作链表向量?
例如,我有一个属性结构(用于链接列表)定义如下:
typedef struct property {
string info;
property* implication;
} property;
然后我有一个类对象:
class objects {
private:
vector<property> *traits;
public:
void giveProperty(property *x) {
traits.push_back(&x);
}
};
从概念上讲,我想要做的就是为对象提供某些属性,每个属性都有一系列含义(这是链接列表),我稍后可以使用它。但我收到错误:
请求“((objects*)this)->objects::traits”中的成员“push_back”,该成员属于非类类型“std::vector >*”
我无法使其正常工作。抱歉,如果不清楚,如果您有疑问,我会尽力澄清。
How can I make a vector of linked lists?
For example I have a struct of properties (for the linked-list) defined as follows:
typedef struct property {
string info;
property* implication;
} property;
And then I have a class object:
class objects {
private:
vector<property> *traits;
public:
void giveProperty(property *x) {
traits.push_back(&x);
}
};
Where what I want to do conceptually is give an object certain properties, and each property has a series of implications (which is the linked list) which I can use later. But I am getting the error:
request for member 'push_back' in '((objects*)this)->objects::traits', which is of non-class type 'std::vector >*'
I am having trouble getting this to work. Sorry if this is unclear, if you have questions I will try clarifying myself.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
在类
objects
中,您声明了一个指向属性向量的指针,而实际上您需要一个属性指针向量:in class
objects
, you have declared a pointer to a vector of properties, when really you want a vector of property pointers:我不确定你为什么使用原始指针。你说你想要一个链表,但你没有在任何地方实现它。为什么不使用
std::list
容器作为链接列表并完全丢失指针呢?I'm not sure why you are using raw pointers. You say you want to have a linked list, but you don't implement that anywhere. Why not use the
std::list
container for your linked lists and lose the pointers altogether?将其更改
为:
或者可能/可能,您希望将其更改为:
更改为:
我会选择后者!
Change this
to this:
Or probably/possibly, you would want to change this:
to this:
I would go for the latter one!
你明白什么是指针吗?
您正在创建一个指向向量的指针,而不是向量。您需要创建一个:
然后将其访问为:
Do you understand what pointers are?
You're creating a pointer to a vector, not a vector. You would need to create one:
then access it as:
更改 -
为
因为您正在尝试将
push_back
地址指向向量traits
。Change -
to
Since you are trying to
push_back
addresses to the vectortraits
.