title: Problems with passing different data types
body: <p>I am working on my own graph project in C++ at the moment and I have come along a problem which I'm sure it's easy to solve but I can't seem to get it right. I am building a template graph class with a vertex class declared inside of it as such:</p>
  
  <pre><code>template &lt;typename T&gt;
  class graph{
  
  public:
  
  class vertex{
             public:
                  bool visited;           //used for paths. True if vertex has been visited
  
                  //vertex constructor
                  vertex(const T&amp; d = T{}, int i = 0): data(d), visited(false), id(i){
                     std::cout &lt;&lt; "Just created a vertex using its constructor :)\n";}
                  //vertex move constructor
                  vertex(T&amp;&amp; d, int i = 0): data(std::move(d)), visited(false), id(i){}
                  //returns vertex ID
                  int returnID() const{ return id; };
                  //returns data in vertex
                  T&amp; operator*(){ return retrieve(); }
                  //returns const reference to data in vertex
                  const T&amp; operator*() const{ return retrieve(); }
                  //returns list of adjacent vertices
                  std::list&lt;vertex&gt;&amp; getList() const{ return adjacent; }
                  //adds vertex to current vertex's adjacency list
                  void addToList(const vertex&amp; add){ 
                     if(!isAdjacent(add))
                          adjacent.push_back(add); 
                  }
                  //returns true if vertices are adjacent
                  bool isAdjacent(const vertex&amp; add){
                     return (find(begin(adjacent), end(adjacent), add) == adjacent.end());
                  }
                  //overloaded equal operator for vertex class
                  bool operator==(const vertex&amp; add){
                     if(data == *add &amp;&amp; id == add.returnID()) return true;
                     return false;
                  }
             private:
                  T data;                 //vertex stores data of any type
                  std::list&lt;vertex&gt; adjacent;     //list of adjacent vertices
                  int id;
  
                  T&amp; retrieve() const{ return data; }
  ];
  
  ];
  </code></pre>
  
  <p>I keep getting this error:</p>
  
  <pre><code>g++ -std=c++11 -Wno-reorder -Wall -pedantic -o executable.x main.cpp 
  In file included from main.cpp:2:0:
  graph.h: In instantiation of ‘T&amp; graph&lt;T&gt;::vertex::retrieve() const [with T = int]’:
  graph.h:70:47:   required from ‘const T&amp; graph&lt;T&gt;::vertex::operator*() const [with T = int]’
  graph.h:84:17:   required from ‘bool graph&lt;T&gt;::vertex::operator==(const graph&lt;T&gt;::vertex&amp;) [with T = int]’
  /usr/lib/gcc/x86_64-pc-linux-gnu/4.8.4/include/g++-v4/bits/stl_algo.h:139:46:   required from ‘_InputIterator std::__find(_InputIterator, _InputIterator, const _Tp&amp;, std::input_iterator_tag) [with _InputIterator = std::_List_iterator&lt;graph&lt;int&gt;::vertex&gt;; _Tp = graph&lt;int&gt;::vertex]’
  /usr/lib/gcc/x86_64-pc-linux-gnu/4.8.4/include/g++-v4/bits/stl_algo.h:4441:45:   required from ‘_IIter std::find(_IIter, _IIter, const _Tp&amp;) [with _IIter = std::_List_iterator&lt;graph&lt;int&gt;::vertex&gt;; _Tp = graph&lt;int&gt;::vertex]’
  graph.h:80:54:   required from ‘bool graph&lt;T&gt;::vertex::isAdjacent(const graph&lt;T&gt;::vertex&amp;) [with T = int]’
  graph.h:75:24:   required from ‘void graph&lt;T&gt;::vertex::addToList(const graph&lt;T&gt;::vertex&amp;) [with T = int]’
  graph.hpp:25:4:   required from ‘bool graph&lt;T&gt;::addEdge(graph&lt;T&gt;::vertex&amp;, graph&lt;T&gt;::vertex&amp;) [with T = int]’
  main.cpp:12:18:   required from here
  graph.h:92:31: error: invalid initialization of reference of type ‘int&amp;’ from expression of type ‘const int’
     T&amp; retrieve() const{ return data; }
                                 ^
  graph.h: In member function ‘T&amp; graph&lt;T&gt;::vertex::retrieve() const [with T = int]’:
  graph.h:92:37: warning: control reaches end of non-void function [-Wreturn-type]
     T&amp; retrieve() const{ return data; }
  </code></pre>
  
  <p>And to sum up the problem it seems to have an issue with my private T&amp; retrieve() function. It keeps saying invalid initialization of type 'int&amp;' from expression with type 'const int' referring to the data it is returning, which is private member data of type T (instantiated in main as an int). Any help would be greatly appreciated! Thanks</p>