Search code examples
c++classtemplatesassignment-operator

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


For part of an assignment, I'm supposed to create an assignment operator for a class called HashGraph that my professor created.

This is what the function prototype looks like:

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

Within this HashGraph<T> class, I have a nested private class called LocalInfo which stores four sets (defined by my professor) and a reference to a HashGraph. Here is the nested private class:

  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

In my assignment operator, I'm supposed to copy the rhs graph into this and return the newly copied graph. My professor says I have to use connect which is in the LocalInfo class, so that each copied LocalInfo object will reset from_graph to the new graph (this).

Here is what my function looks like right now:

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

This however does not compile because of the line temp.connect(rhs), where it says there is no matching function call to HashGraph<int>::LocalInfo::connect(const HashGraph<int>&).

The way my professor has it set up is that this->clear() does empty the this HashGraph. To copy the node_values map over, I use his iterator to iterate through the rhs.node_values map.

As a note, he has it set up so that calling node_values[i.first] = temp will actually create a key in node_values which is the key from the right hand side, and will then assign the value temp (which is a LocalInfo object) into that key.

But like I said, this doesn't compile. So how can I use connect() properly?


Solution

  • Are you sure you want to connect to rhs and not to this? rhs is const HashGraph<int> & which make you don't have an access to modify to the structure.