title: C++ error - "member initializer expression list treated as compound expression"
body: <p>I'm getting a C++ compiler error which I'm not familiar with. Probably a really stupid mistake, but I can't quite put my finger on it.</p>
  
  <p>Error:</p>
  
  <pre><code>test.cpp:27: error: member initializer expression list treated as compound expression
  test.cpp:27: warning: left-hand operand of comma has no effect
  test.cpp:27: error: invalid initialization of reference of type ‘const Bar&amp;’ from expression of type ‘int’
  </code></pre>
  
  <p>Code:</p>
  
  <pre><code>  1 #include &lt;iostream&gt;
    2
    3 class Foo {
    4 public:
    5         Foo(float f) :
    6                 m_f(f)
    7         {}
    8
    9         float m_f;
   10 };
   11
   12 class Bar {
   13 public:
   14         Bar(const Foo&amp; foo, int i) :
   15                 m_foo(foo),
   16                 m_i(i)
   17         {}
   18
   19         const Foo&amp; m_foo;
   20         int m_i;
   21 };
   22
   23
   24 class Baz {
   25 public:
   26         Baz(const Foo&amp; foo, int a) :
   27                 m_bar(foo, a)
   28         {}
   29
   30         const Bar&amp; m_bar;
   31 };
   32
   33 int main(int argc, char *argv[]) {
   34         Foo a(3.14);
   35         Baz b(a, 5.0);
   36
   37         std::cout &lt;&lt; b.m_bar.m_i &lt;&lt; " " &lt;&lt; b.m_bar.m_foo.m_f &lt;&lt; std::endl;
   38
   39         return 0;
   40 }
  </code></pre>
  
  <p>Note:
    It looks like the compiler is evaluating the commas in line 27 like here:
  <a href="http://publib.boulder.ibm.com/infocenter/lnxpcomp/v8v101/index.jsp?topic=/com.ibm.xlcpp8l.doc/language/ref/co.htm" rel="noreferrer">http://publib.boulder.ibm.com/infocenter/lnxpcomp/v8v101/index.jsp?topic=/com.ibm.xlcpp8l.doc/language/ref/co.htm</a></p>
  
  <p><strong>edit:</strong>
  Okay, I understand the problem as Alan explained it. Now, for extra imaginary points, can someone explain how the compiler (g++) came up with the error message it gave?</p>