繁体   English   中英

从外部类C ++正确调用嵌套类中的函数

[英]Correctly call a function in a nested class from an outer class C++

对于作业的一部分,我应该为我的教授创建的名为HashGraph的类创建一个作业运算符。

函数原型如下所示:

HashGraph<T>& operator = (const HashGraph<T>& rhs);

在此HashGraph<T>类中,我有一个名为LocalInfo的嵌套私有类,该类存储四组(由我的教授定义)和对HashGraph的引用。 这是嵌套的私有类:

  private:
    //All methods and operators relating to LocalInfo are defined below, followed by
    //  friend functions for insertion onto output streams of HashGrah and LocalInfo
    class LocalInfo {
      public:
        LocalInfo()                : from_graph(nullptr), out_nodes(hash_str), in_nodes(hash_str), out_edges(hash_pair_str), in_edges(hash_pair_str) {}
        LocalInfo(HashGraph<T>* g) : from_graph(g),       out_nodes(hash_str), in_nodes(hash_str), out_edges(hash_pair_str), in_edges(hash_pair_str) {}
        void connect(HashGraph<T>* g) {from_graph = g;}

        bool operator == (const LocalInfo& rhs) const {
          return this->in_nodes == rhs.in_nodes && this->out_nodes == rhs.out_nodes &&
                 this->in_edges == rhs.in_edges && this->out_edges == rhs.out_edges;
        }
        bool operator != (const LocalInfo& rhs) const {
          return !(*this == rhs);
        }

        //from_graph should point to the HashGraph LocalInfo is in, so LocalInfo
        //  methods (see <<)
        HashGraph<T>* from_graph;
        ics::HashSet<std::string>                        out_nodes;
        ics::HashSet<std::string>                        in_nodes;
        ics::HashSet<ics::pair<std::string,std::string>> out_edges;
        ics::HashSet<ics::pair<std::string,std::string>> in_edges;
   };//LocalInfo

在我的赋值运算符,我应该在复制rhs图到this并返回新复制的图形。 我的教授说,我必须使用connect这是在LocalInfo类,让每个复制LocalInfo对象将重置from_graph新图( this )。

这是我的函数现在的样子:

template<class T>
HashGraph<T>& HashGraph<T>::operator = (const HashGraph<T>& rhs){
    this->clear();
    for(auto i : rhs.node_values) {
        HashGraph<T>::LocalInfo temp;
        temp.connect(rhs);
        node_values[i.first] = temp; 
    }
    edge_values = rhs.edge_values;
    return *this;
}

但是,由于行temp.connect(rhs)无法编译,它表示no matching function call to HashGraph<int>::LocalInfo::connect(const HashGraph<int>&)

我的教授设置它的方式是this->clear()确实清空了this HashGraph。 要复制node_values映射,我使用他的迭代器迭代rhs.node_values映射。

需要注意的是,他进行了设置,以便调用node_values[i.first] = temp实际上会在node_values创建一个键,该键是从右侧开始的键,然后分配值temp (这是LocalInfo对象)插入该键。

但是就像我说的那样,这不能编译。 那么如何正确使用connect()

该函数需要一个指针,而不是对象或引用。

temp.connect(&rhs);

您确定要连接到rhs而不是this吗? rhs是const HashGraph<int> &它使您无权修改结构。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM