title: C++ friend function can't access private members
body: <p>This is supposed to be a string class with a bunch of operators and functions, including two friend functions. And those two cause some trouble for me, because the compiler says that they can not access the private members. Here is my string.h:</p>
  
  <pre><code>#include &lt;iostream&gt;
  #ifndef STR_H
  #define STR_H
  
  namespace MyStr
  {
  class Str
  {
  private:
      unsigned int length;
      char *data;
  public:
      Str();
      Str(const Str&amp;);
      Str(const char*);
      Str(char c, unsigned int db);
      ~Str();
      char* cStr() const;
      unsigned int getLength() const;
  </code></pre>
  
  <p>lots of irrevelant functions here...</p>
  
  <pre><code>    friend int operator/ (const Str&amp;, char);
      friend std::ostream&amp; operator&lt;&lt; (std::ostream&amp;, const Str&amp;);
  };
  }
  #endif /* STR_H */
  </code></pre>
  
  <p>here is the main.cpp:</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include "Str.h"
  
  using namespace std;
  using namespace MyStr;
  
  ostream&amp; operator&lt;&lt; (ostream&amp; out,const Str&amp; str)
  {
      for (int i=0; i&lt;str.length; i++)
      {
          out&lt;&lt;str.data[i];
      }
      out&lt;&lt;endl;
      return out;
  }
  
  int operator/ (const Str&amp; str, char c)
  {
      for (int i=0; i&lt;str.length; i++)
      {
          if(str.data[i]==c) return i;
      }
      return -1;
  }
  </code></pre>
  
  <p>This code won't compile, the compiler claiming that the <code>Str</code> members are private.</p>