title: c++ undefined reference in extraction operator overloading
body: <p>Learning c++ bit by bit in codeblocks, I encountered this error. I have tried it for hours and saw various links but its still not working,</p>
  
  <p><strong>main.cpp</strong></p>
  
  <pre><code>#include &lt;iostream&gt; 
  #include "Person.h"
  
  using namespace std;
  
  int main()
  {
    foo:: Person p;
    //std:: cin &gt;&gt; p;
    std:: cout &lt;&lt; p;
    return 0;
   }
  </code></pre>
  
  <p><strong>Person.h</strong></p>
  
  <pre><code>#ifndef PERSON_H
  #define PERSON_H
  
  namespace foo{
  class Person
  {
     public:
     int age;
     Person();
     friend std:: ostream&amp; operator &lt;&lt; (std::ostream&amp;,const Person&amp;);
   //friend std:: istream&amp; operator &lt;&lt; (std:: istream&amp; in, Person&amp; p);
     protected:
     private:
  
    // std::string name;
  };
  
  }
  #endif // PERSON_H
  </code></pre>
  
  <p><strong>Person.cpp</strong></p>
  
  <pre><code>#include &lt;iostream&gt;
  #include "Person.h"
  
  using namespace foo;
  
  Person::Person()
  {
     age = 0;
    //name = "noname";
  }
  
  
  std:: ostream&amp; operator &lt;&lt;(std:: ostream&amp; out, Person const &amp;p)
  {
    out &lt;&lt; "Extraction operator" &lt;&lt; std:: endl &lt;&lt; p.age &lt;&lt; std:: endl;
    // out &lt;&lt; p.name &lt;&lt; std::endl;
    return out;
  }
  
  /*std:: istream&amp; operator &gt;&gt;(std:: istream&amp; in, Person &amp;p)
  {
    in &gt;&gt; p.age &gt;&gt; p.name;
    return in;
  }*/
  </code></pre>
  
  <p>Q1. Why do we need to create the header file and Person.cpp in a seperate namespace?
  Guess: Is it because cout would simply mean the global namespace and then we again have a overloaded cout so which definition is the compiler going to call its not sure?</p>
  
  <p>Q2. By creating a object p of Person class in foo namespace in main and calling std:: cout &lt;&lt; p, what are we trying to do? (std is in std namespace and we want to call the overloaded operator)</p>
  
  <p>Error:</p>
  
  <pre><code>undefined reference to foo:: operator&lt;&lt;(std::ostream&amp;, foo::Person const&amp;)') 
  </code></pre>
  
  <p>If we write the age in private it says that its is private but being a friend it should have access to private members. I know it's something related to namespace but I can't find out the reason.</p>
  
  <p><a href="https://stackoverflow.com/questions/20218520/no-match-for-operator-in-stdcin">no match for operator &gt;&gt; in std::cin</a>
  Earlier it was giving the above and many other error so I tried using two namespaces but still its not working.</p>