【问题标题】:Making a vector of linked lists?制作链表的向量?
【发布时间】:2011-03-19 20:33:09
【问题描述】:

如何制作链表向量?

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

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 >*'

我无法让它工作。抱歉,如果不清楚,如果您有任何问题,我会尝试澄清自己。

【问题讨论】:

    标签: c++ vector linked-list


    【解决方案1】:

    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
        }
    };
    

    【讨论】:

      【解决方案2】:

      我不确定您为什么使用原始指针。你说你想要一个链表,但你没有在任何地方实现它。为什么不将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"));
      }
      

      【讨论】:

        【解决方案3】:

        改变这个

        traits.push_back(&x);
        

        到这里:

        traits->push_back(*x);
        

        或者可能/可能,你会想要改变这个:

        vector<property> *traits;
        

        到这里:

        vector<property*> traits;
        

        我会选择后者!

        【讨论】:

        • 谢谢,但我仍然收到错误消息:no matching function for call to 'std::vector&lt;property*, std::allocator&lt;property*&gt; &gt;::push_back(property**)' 完成两项更改后。
        • @anon:不。改变第一或第二。不是都。我没说,两个都改。我说“把这个改成那个”或“这个改成那个”
        【解决方案4】:

        你了解什么是指针吗?

        vector<property*> *traits; 
        

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

        traits = new vector<property*>;
        

        然后访问它:

        traits->push_back(x);
        

        【讨论】:

          【解决方案5】:

          改变-

          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*
          
          }
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2012-05-26
            • 1970-01-01
            • 2013-10-03
            • 1970-01-01
            • 2017-12-25
            相关资源
            最近更新 更多