title: Ambiguous overload for ‘operator<<’ in ‘std::cout <<
body: <p>I have the following <code>main.cpp</code> file</p>
  
  <pre><code>#include "listtemplate.h"
  //#include &lt;iostream&gt;
  using namespace std;
  
  int main()
  {
      int UserChoice;
      cout &lt;&lt; "Hello, World!" &lt;&lt; endl;
      cin &gt;&gt; UserChoice;
      cout &lt;&lt; UserChoice;
  }
  </code></pre>
  
  <p>In it's current form, everything works. I enter an integer, and that integer is printed to the screen. However, when I uncomment the <code>cout &lt;&lt; "Hello, World!" &lt;&lt; endl</code> line, I get the following error </p>
  
  <pre><code>main.cpp:10: error: ambiguous overload for ‘operator&lt;&lt;’ in ‘std::cout &lt;&lt; "Hello, World!"’
  </code></pre>
  
  <p>I can also make it work by commenting out #include "listtemplate.h", uncommenting the hello world line, and including <code>&lt;iostream&gt;</code> in main (currently accessible through the template. Can anyone see what I'm missing here?</p>
  
  <p>listtemplate.h</p>
  
  <pre><code>#ifndef LISTTEMPLATE_H
  #define LISTTEMPLATE_H
  #include "list.h"
  using namespace std;
  
  // Default constructor
  template &lt;class Type&gt;
  list&lt;Type&gt; :: list() : Head(NULL) {}
  
  // Destructor
  template &lt;class Type&gt;
  list&lt;Type&gt; :: ~list()
  {
      Node *Temp;
      while (Head != NULL)
      {
          Temp = Head;
          Head = Head -&gt; Next;
          delete Temp;
      }
  }
  
  // Copy constructor
  template &lt;class Type&gt;
  list&lt;Type&gt; :: list (const Type&amp; OriginalList)
  {
      Node *Marker;
      Node *OriginalMarker;
  
      OriginalMarker = OriginalList.Gead;
      if (OriginalMarker == NULL) Head = NULL;
      else
      {
          Head = new Node (OriginalMarker -&gt; Element, NULL);
          Marker = Head;
          OriginalMarker = OriginalMarker -&gt; Next;
  
          while (OriginalMarker != NULL)
          {
              Marker -&gt; Next = new Node (OriginalMarker -&gt; Next);
              OriginalMarker = OriginalMarker -&gt; Next;
              Marker = Marker -&gt; Next;
          }
      }
  }
  
  // Copy assignment operator
  template &lt;class Type&gt;
  list&lt;Type&gt;&amp; list&lt;Type&gt; :: operator= (const list&lt;Type&gt;&amp; Original)
  {
      Node *Marker;
      Node *OriginalMarker;
  
      // Check that we are not assigning a variable to itself
      if (this != &amp;Original)
      {
          // First clear the current list, if any
          while (Head != NULL)
          {
              Marker = Head;
              Head = Head -&gt; Next;
              delete Marker;
          }
  
          // Now build a new copy
          OriginalMarker = Original.Head;
          if (OriginalMarker == NULL) Head = NULL;
          else
          {
              Head = new Node (OriginalMarker -&gt; Element, NULL);
              Marker = Head;
              OriginalMarker = OriginalMarker -&gt; Next;
  
              while (OriginalMarker != NULL)
              {
                  Marker -&gt; Next = new Node (OriginalMarker -&gt; Element, NULL);
                  OriginalMarker = OriginalMarker -&gt; Next;
                  Marker = Marker -&gt; Next;
              }
          }
      }
      return (*this);
  }
  
  // Test for emptiness
  template &lt;class Type&gt;
  bool list&lt;Type&gt; :: Empty() const
  {
      return (Head == NULL) ? true : false;
  }
  
  // Insert new element at beginning
  template &lt;class Type&gt;
  bool list&lt;Type&gt; :: Insert (const Type&amp; NewElement)
  {
      Node *NewNode;
      NewNode = new Node;
      NewNode -&gt; Element = NewElement;
      NewNode -&gt; Next = Head;
      return true;
  }
  
  // Delete an element
  template &lt;class Type&gt;
  bool list&lt;Type&gt; :: Delete (const Type&amp; DelElement)
  {
      Node *Temp;
      Node *Previous;
  
      // If list is empty
      if (Empty()) return false;
  
      // If element to delete is the first one
      else if (Head -&gt; Element == DelElement)
      {
          Temp = Head;
          Head = Head -&gt; Next;
          delete Temp;
          return true;
      }
  
      // If the list has only one element which isn't the specified element
      else if (Head -&gt; Next == NULL) return false;
  
      // Else, search the list element by element to find the specified element
      else
      {
          Previous = Head;
          Temp = Head -&gt; Next;
  
          while ((Temp -&gt; Element != DelElement) &amp;&amp; (Temp -&gt; NExt != NULL))
          {
              Previous = Temp;
              Temp = Temp -&gt; Next;
          }
  
          if (Temp -&gt; Element == DelElement)
          {
              Previous -&gt; Next = Temp -&gt; Next;
              delete Temp;
              return true;
          }
          else return false;
      }
  }
  
  // Print the contents of the list
  template &lt;class Type&gt;
  void list&lt;Type&gt; :: Print (ostream&amp; OutStream) const
  {
      Node *Temp;
      Temp = Head;
  
      while (Temp != NULL)
      {
          OutStream &lt;&lt; Temp -&gt; Element &lt;&lt; " ";
          Temp = Temp -&gt; Next;
      }
  }
  
  // Overloaded output operator
  template &lt;class Type&gt;
  ostream&amp; operator&lt;&lt; (ostream&amp; OutStream, const list&lt;Type&gt;&amp; OutList)
  {
      OutList.Print (OutStream);
      return OutStream;
  }
  #endif
  </code></pre>
  
  <p>list.h</p>
  
  <pre><code>#ifndef LIST_H
  #define LIST_H
  #include &lt;iostream&gt;
  #include &lt;cstddef&gt;
  using namespace std;
  
  template &lt;class Type&gt;
  class list
  {
  private:
      struct Node
      {
      public:
          Type Element;
          Node *Next;
  
          Node() : Next(NULL) {} // Default constructor
          Node (Type Data, Node *PNode = NULL) : // Non-default constructor
              Element (Data),
              Next (PNode) {}
      };
  
      Node *Head;
  public:
      list();
      ~list();
      list (const Type&amp; OriginalList);
      bool Empty() const;
      bool Insert (const Type&amp; NewElement);
      bool Delete (const Type&amp; DelElement);
      void Print (ostream&amp; OutStream) const;
      list&amp; operator= (const list&lt;Type&gt;&amp; Original);
  };
  
  template &lt;class Type&gt;
  ostream&amp; operator&lt;&lt; (ostream&amp; OutStream, const Type&amp; OutList);
  #endif
  </code></pre>