如何将`this`指针从外部类传递到成员结构?

时间:2019-05-09 08:02:18

标签: c++

我有一个如下所示的模板类。我在行_iterator处收到错误消息“ ElemsCombineCycle没有名为auto comb = ElemsCombineCycle ();的成员”,原因是结构内部的this没有指向外部类。

template < typename G, typename A, typename Gt >
class WIt final : public WCV< G, Gt > {
  private:
    InterIt& _iterator;
  public:
    using Elems = std::vector<AVC>;

    struct ElemsCombineCycle {
      Node head;
      const WNT& cycle_n = this->_iterator.nest(head);

      template<typename Iterator>
      Elems operator()(const Elems acc, const Iterator& it) const {
      Node pred = *it;
      Elems post_vector;
      post_vector = this->_iterator.post(pred);

      if (this->_iterator.nest(pred) <= cycle_n) {

      Elems result;
      for (auto a : acc) {
        for (auto n : post_vector) {
          A pre = a.get_value();
             result.push_back(AVC(pre.join(this->_iterator.analyze(head, n.get_value()))));

          }
        }
       return result;
     }
    else
       return acc;
   }
 };

 void visit(const WCT& c) override {
   Node head = c.head();
   A pre = A::bottom();
   Elems pre_vector;
   pre_vector.push_back(AVC(pre));

   const WNT& cycle_n = this->_iterator.nest(head);

   this->_iterator.notify(head);

   auto comb = ElemsCombineCycle ();
   comb.head = head;
   Elems new_pre_vector = std::accumulate(Gt::p_begin(head), Gt::p_end(head), pre_vector, comb);
  }
 };

请告诉我如何解决此错误,并将this从外部类传递到内部结构。

1 个答案:

答案 0 :(得分:2)

C ++不是Java。

在类的声明中声明结构不会使内部指针指向外部,没有WIt实例与ElemsCombineCycle实例相关联,它们可以独立存在。

您似乎正在尝试编写等效于lambda的代码,因此只需编写一个lambda。那些 do this

有特殊待遇
void visit(const WCT& c) override {
   Node head = c.head();
   A pre = A::bottom();
   Elems pre_vector;
   pre_vector.push_back(AVC(pre));

   const WNT& cycle_n = this->_iterator.nest(head);

   this->_iterator.notify(head);

   auto comb = [this, cycle_n, head](const Elems acc, const Iterator& it) {
      Node pred = *it;
      Elems post_vector = this->_iterator.post(pred);

      if (this->_iterator.nest(pred) <= cycle_n) {

      Elems result;
      for (auto a : acc) {
        for (auto n : post_vector) {
          A pre = a.get_value();
             result.push_back(AVC(pre.join(this->_iterator.analyze(head, n.get_value()))));

          }
        }
       return result;
     }
    else
       return acc;
   };
   Elems new_pre_vector = std::accumulate(Gt::p_begin(head), Gt::p_end(head), pre_vector, comb);
  }