C++: error when have private copy ctor with public assignment operator
<p>Can one of you explain why the following piece of code does not compile?</p>

<pre><code>#include &lt;iostream&gt;

using namespace std;

class Foo
{
public:
  Foo() { cout &lt;&lt; "Foo::Foo()" &lt;&lt; endl &lt;&lt; endl; }
  Foo&amp; operator=(const Foo&amp;) { cout &lt;&lt; "Foo::operator=(const Foo&amp;)" &lt;&lt; endl &lt;&lt; endl; }
private:
  Foo(const Foo&amp; b) { *this = b; cout &lt;&lt; "Foo::Foo(const Foo&amp;)" &lt;&lt; endl &lt;&lt; endl; }
};

int main()
{
  Foo foo;

  foo = Foo();
}
</code></pre>

<p>The error I receive:</p>

<pre><code>$ g++ -o copy_ctor_assign copy_ctor_assign.cc &amp;&amp; ./copy_ctor_assign
copy_ctor_assign.cc: In function 'int main()':
copy_ctor_assign.cc:10: error: 'Foo::Foo(const Foo&amp;)' is private
copy_ctor_assign.cc:17: error: within this context
</code></pre>

<p>Note: when I remove the <strong><em>private:</em></strong> keyword the code compiles but the copy ctor is never called. So why does it err when it's private?</p>

<p>Not sure if it's important but I'm using:</p>

<pre><code>$ g++ --version
g++ (GCC) 4.1.2 20080704 (Red Hat 4.1.2-44)
Copyright (C) 2006 Free Software Foundation, Inc.
</code></pre>
