制作链表向量?

发布于 2024-10-24 09:00:28 字数 579 浏览 1 评论 0原文

如何制作链表向量?

例如,我有一个属性结构(用于链接列表)定义如下:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

一花一树开 2024-10-31 09:00:28

在类 objects 中,您声明了一个指向属性向量的指针,而实际上您需要一个属性指针向量:

class objects {
private:
    vector<property*> traits;    // Note where * is
public:
    void giveProperty(property *x) {
        traits.push_back(x);    // Note that x is already a pointer, no need to take its address
    }
};

in class objects, you have declared a pointer to a vector of properties, when really you want a vector of property pointers:

class objects {
private:
    vector<property*> traits;    // Note where * is
public:
    void giveProperty(property *x) {
        traits.push_back(x);    // Note that x is already a pointer, no need to take its address
    }
};
放赐 2024-10-31 09:00:28

我不确定你为什么使用原始指针。你说你想要一个链表,但你没有在任何地方实现它。为什么不使用 std::list 容器作为链接列表并完全丢失指针呢?

#include <string>
#include <vector>
#include <list>

using std::string;
using std::vector;
using std::list;

struct implication {
    implication(string name) : name(name) {}
    string name;
};

struct property {
    property(string info) : info(info) {}
    string info;
    list<implication> implList;
};

class object {
private:
    vector<property> traits;
public:
    void giveProperty(const property& x) {
        traits.push_back(x);
    }
};

int f() {
    object o;
    property p1("p1");
    property p2("p2");
    implication i("implic");

    p1.implList.push_back(i);
    p1.implList.push_back(implication("other implic"));

    o.giveProperty(p1);

    o.giveProperty(property("p3"));
}

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?

#include <string>
#include <vector>
#include <list>

using std::string;
using std::vector;
using std::list;

struct implication {
    implication(string name) : name(name) {}
    string name;
};

struct property {
    property(string info) : info(info) {}
    string info;
    list<implication> implList;
};

class object {
private:
    vector<property> traits;
public:
    void giveProperty(const property& x) {
        traits.push_back(x);
    }
};

int f() {
    object o;
    property p1("p1");
    property p2("p2");
    implication i("implic");

    p1.implList.push_back(i);
    p1.implList.push_back(implication("other implic"));

    o.giveProperty(p1);

    o.giveProperty(property("p3"));
}
你是年少的欢喜 2024-10-31 09:00:28

将其更改

traits.push_back(&x);

为:

traits->push_back(*x);

或者可能/可能,您希望将其更改为:

vector<property> *traits;

更改为:

vector<property*> traits;

我会选择后者!

Change this

traits.push_back(&x);

to this:

traits->push_back(*x);

Or probably/possibly, you would want to change this:

vector<property> *traits;

to this:

vector<property*> traits;

I would go for the latter one!

岁月苍老的讽刺 2024-10-31 09:00:28

你明白什么是指针吗?

vector<property*> *traits; 

您正在创建一个指向向量的指针,而不是向量。您需要创建一个:

traits = new vector<property*>;

然后将其访问为:

traits->push_back(x);

Do you understand what pointers are?

vector<property*> *traits; 

You're creating a pointer to a vector, not a vector. You would need to create one:

traits = new vector<property*>;

then access it as:

traits->push_back(x);
留一抹残留的笑 2024-10-31 09:00:28

更改 -

vector<property> *traits;

vector<property*>  traits; // Needs to be the declaration.

因为您正在尝试将 push_back 地址指向向量 traits

void giveProperty(property *x) {
    traits.push_back(&x);
                     ^ Error : With the above modifications made, traits can hold
                       elements of type property*. By doing &x, you are trying to do
                       push_back pointer's address which in that case vector should
                       hold elements of type property**. So, just remove the & 
                       symbol before x while push_back because x is already of type
                       property*

}

Change -

vector<property> *traits;

to

vector<property*>  traits; // Needs to be the declaration.

Since you are trying to push_back addresses to the vector traits.

void giveProperty(property *x) {
    traits.push_back(&x);
                     ^ Error : With the above modifications made, traits can hold
                       elements of type property*. By doing &x, you are trying to do
                       push_back pointer's address which in that case vector should
                       hold elements of type property**. So, just remove the & 
                       symbol before x while push_back because x is already of type
                       property*

}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文