title: c++: Access to private variables in abstract base class
body: <p>long-time lurker, first-time asker. I'm doing a project on the TSP (Traveling Salesman Problem) in c++ and I have a small question. I need to take two nodes (called NodeIDs in this scenario, they are unsigned ints) and add an edge between them in the addEdge method. The signature along with some notes from my teacher (I will fill in the preconditions later):</p>
  
  <pre><code>/* 
   * Add a weighted, undirected edge between nodes u and v.
   * 
   * Preconditions: 
   *     u and v are legal labels (i.e. 0 &lt;= u &lt; G.size(), 0 &lt;= v &lt; G.size())
   *     u != v
   *     There is no edge between u and v.
   *     weight &gt; 0
   */
  void ListGraph::addEdge(NodeID u, NodeID v, EdgeWeight weight) {
  
      /* Code here */
  
  }
  </code></pre>
  
  <p>Now, the way I see myself coding this is referencing the list of edges and nodes in the base class. However, the base class is abstract, and the list is private. Here is the entirety of that base class.
  NOTE: THIS CLASS MUST STAY AS IS (it's the teacher's class and one of the few in the project I must remain faithful to and use without changing it.)
  Here it is:</p>
  
  <pre><code>#include &lt;vector&gt;
  #include "Graph.h"
  
  #pragma once
  
  typedef std::list&lt;NWPair&gt; EList;
  
  class ListGraph : public Graph {
  public:
      ListGraph(int numNodes);
      ~ListGraph();
  
      // Modifiers
      virtual void addEdge(NodeID u, NodeID v, EdgeWeight weight);
  
      // Inspectors
      virtual EdgeWeight weight(NodeID u, NodeID v) const;
      virtual std::list&lt;NWPair&gt; getAdj(NodeID u) const;
      virtual unsigned degree(NodeID u) const;
      virtual unsigned size() const;
      virtual unsigned numEdges() const;
  
  private:
      ListGraph() {;}
  
      std::vector&lt;EList&gt; edgeList;
      int num_edges;
  };
  </code></pre>
  
  <p>The only way I can think to add a weight to the edge between nodes is to access this edgeList and change it. Any suggestions would be quite helpful.</p>
  
  <p>Here is all of Graph.h if anyone needs it:</p>
  
  <pre><code>#include &lt;utility&gt;
  #include &lt;list&gt;
  
  #pragma once
  
   /*
   * Useful typedefs, primarily for the purpose of read-ability.
   */
  typedef unsigned NodeID;
  typedef double EdgeWeight;
  typedef std::pair&lt;NodeID, EdgeWeight&gt; NWPair;
  
  
  class Graph {
   public:
    virtual ~Graph() {}
  
    /*
     * Add a weighted, undirected edge between nodes u and v.
     * 
     * Preconditions: 
     *     u and v are legal labels (i.e. 0 &lt;= u &lt; G.size(), 0 &lt;= v &lt; G.size())
     *     u != v
     *     There is no edge between u and v.
     *     weight &gt; 0
     */
    virtual void addEdge(NodeID u, NodeID v, EdgeWeight weight) = 0;
  
  
    /*
     * Get the weight between nodes u and v; return 0 if there is no edge.
     *
     * Preconditions: 
     *     u and v are legal labels (i.e. 0 &lt;= u &lt; G.size(), 0 &lt;= v &lt; G.size())
     */
    virtual EdgeWeight weight(NodeID u, NodeID v) const = 0;
  
    /*
     * Return a list of NodeID/EdgeWeight pairs describing the nodes adjacent to edge w.
     *
     * Preconditions: u is a legal label.
     */
    virtual std::list&lt;NWPair&gt; getAdj(NodeID u) const = 0;
  
    /*
     * Return the degree (i.e. the number of neighorbs) of node u.
     *
     * Preconditions: u is a legal label;
     */
    virtual unsigned degree(NodeID u) const = 0;
  
    /*
     * Return the number of nodes in the graph.
     */
    virtual unsigned size() const = 0;
  
    /* 
     * Return the number of edges in the graph.
     */
    virtual unsigned numEdges() const = 0;
  };
  </code></pre>
  
  <p>As I said, ANY help would be greatly appreciated. Thanks!</p>
  
  <p>edit: trying to reference edgeList with "ListGraph::edgeList" in the method "ListGraph::addEdge" results in an error. When I hold my cursor over, it says this:
  Error: class "ListGraph" has no member "edgeList"</p>
  
  <p>edit 2: this code is in three files.
  Graph.h: unchangeable, abstract; where all methods are
  ListGraph.h: unchangeable, mostly abstract; where edgeList and num_edges are (these are private)
  ListGraph.cpp: where I will put my code, and seemingly where I cannot access private variables such as edgeList and num_edges</p>
  
  <p>edit 3: I'M AN IDIOT, I put all of the methods in ListGraph.cpp in a class called ListGraph, so the program was searching ListGraph.cpp rather than ListGraph.h. Sorry, wasted yo time, go ahead and delete this if you wish or I will soon. Thanks guys, sorry again.</p>