title: Store the address of an object inside a node
body: <p>I'm trying to create an object of a class called Cell and store it in a linked list. I'm sure I could do this with an array, but part of my assignment is that I use a linked list and I didn't think I'd get this many problems. This is currently my node. Right now, I have all these variables stored in the node, but I'd rather create an object(Called "Cell") to store them. Info should be a pointer to an object of type T. Right now, that T should be of type Cell.</p>
  
  <pre><code>template&lt;class T&gt;
  struct Node {
      T *info; 
      Node&lt;T&gt; *nodeP;
      Node&lt;T&gt; *linkP;
  
      int nodeNumber = 0;
      bool purchased = false;
  
      std::string color = " ";
      int index = 0;
      int max_num = 0;
      std::string name = " ";
      int price;
  };
  </code></pre>
  
  <p>In here I am creating the node and adding it to a linked list. At the moment I'm just filling in values of the node, but I'm trying to create an object of type Cell and assign it's address to the pointer info. I've tried a couple different ways but keep coming back with errors. I commented them out so you can see what I've tried.</p>
  
  <pre><code>template&lt;class T&gt;
  void Board&lt;T&gt;::setCellValue() {
  
      //open file
      ifstream inFile;
      string line;
      inFile.open("CellValues.txt");
  
      //Check for Error
      if (inFile.fail()) {
          cerr &lt;&lt; "File does not exist!";
          exit(1);
      }
  
      int index = 0, max_num = 0, count = 0, price = 0;
      string color, name;
  
      istringstream inStream;
      while (getline(inFile, line)) {
          inStream.clear();
          inStream.str(line);
  
          inStream &gt;&gt; color &gt;&gt; index &gt;&gt; max_num &gt;&gt; name &gt;&gt; price;
  
  
          //creates node
          Node&lt;T&gt; *newNodeP = new Node&lt;T&gt;;
  
          //create pointer, assign pointer to pointer in Node
          //Cell&lt;T&gt; *cellPtr = new Cell&lt;T&gt;(count, name, color, index, max_num, price);
          //newNode-&gt;info= cellPtr;
          //creating anonymous object and assigning to the node? I think
          newNodeP-&gt;info = new Cell&lt;T&gt;(color, index, max_num, name, price);
          //weird way I was just experimenting with
          newNodeP-&gt;info-&gt;Cell&lt;T&gt;(count, name, color, index, max_num, price);
  
          //fills node values(this is what I want to handle in the object
          newNodeP-&gt;color = color;
          newNodeP-&gt;index = index;
          newNodeP-&gt;max_num = max_num;
          newNodeP-&gt;name = name;
          newNodeP-&gt;nodeNumber += count;
          newNodeP-&gt;price = price;
  
          newNodeP-&gt;linkP = NULL;
  
          if (firstP != NULL)
              lastP-&gt;linkP = newNodeP;
          else
              firstP = newNodeP;
  
          lastP = newNodeP;
  
          count++;
      }   
  }
  </code></pre>
  
  <p>Currently, I have two ways of returning the node landed on. One returns a Node* and sort of works. It returns the pointer to the node, and I can access the values inside that node, but I can't figure out how to store the pointer to that node.</p>
  
  <pre><code>//Find Cell
  template&lt;class T&gt;
  Node&lt;T&gt;* Board&lt;T&gt;::findCell(int id) {
  
      for (Node&lt;T&gt; *traverseP = firstP; traverseP != NULL; traverseP = traverseP-&gt;linkP) {
          if (traverseP-&gt;nodeNumber == id) {
              return traverseP;
          }
      }
      return nullptr;
  }
  
  //how I call it in main. it returns an address to that node, but I'm getting errors trying to store that address in a pointer.
  cout &lt;&lt; "You landed on cell " &lt;&lt; gameBoard.findCell(player.getCellNum()) &lt;&lt; endl &lt;&lt; endl;
  Node&lt;T&gt; *ptr = gameboard.findCell(player.getCellNum())-&gt;info;
  </code></pre>
  
  <p>This second way, I think returns the reference to the object in the node, but my earlier problem is stopping me from figuring that out.</p>
  
  <pre><code>//Return Cell
  template &lt;class T&gt;
  T Board&lt;T&gt;::returnCell(int id) {
      for (Node&lt;T&gt; *traverseP = firstP; traverseP != NULL; traverseP = traverseP-&gt;linkP) {
          if (traverseP-&gt;nodeNumber == id) {
              return traverseP-&gt;info;
          }
      }
      return nullptr;
  }
  //How i'm calling it in main. I don't really know what it's returning though because it only prints "You landed on " and then nothing else. 
      cout &lt;&lt; "You landed on " &lt;&lt; gameBoard.returnCell(player.getCellNum()) &lt;&lt; endl;
  </code></pre>