#include <iostream> struct B1 { B1(int i):i_(i) { } int i_; }; struct B2 { B2 (void) { } }; struct Initializer { B1 b1(void) const { return B1(1); } B2 b2(void) const { return B2(); } }; struct D : public B1, public B2 { D ( const Initializer &init ) : B1(init.b1()), B2(init.b2()) { } }; int main (void) { d(Initializer()); // The line above produces the following error in g++ 3.4.4 under cygwin: // bug.cpp: In function `int main()': // bug.cpp:35: error: request for member `i_' in `d', which is of non-class type `D // ()(Initializer (*)())' // Commenting out that line and uncommenting the following two lines: // Initializer init; // D d(init); // compiles without error in g++ 3.4.4 under cygwin, but produces the output: // i_=0 // Under MS Visual C++ 6.0 either one compiles without error and produces: // i_=1 // which appears to be correct. std::cout << "i_=" << d.i_ << std::endl; return 0; }
Somehow when I copied and pasted the first line in main came out wrong; it should be: D d(Initializer()); which produces the apparently incorrect error as noted.
D d(Initializer()); parses as a function declaration, as you see from the error (which is on the cout line btw.): // bug.cpp: In function `int main()': // bug.cpp:35: error: request for member `i_' in `d', which is of non-class type `D // ()(Initializer (*)())' use D d((Initializer()));
Subject: Re: Initializing using methods of class object passed to constructor On 30 Jan 2006 10:29:01 -0000, you wrote: > > >------- Comment #2 from rguenth at gcc dot gnu dot org 2006-01-30 10:29 ------- >D d(Initializer()); > >parses as a function declaration, as you see from the error (which is on >the cout line btw.): Sorry, I don't get it. If the line were something like: D d(Initializer init); or D d(Initializer); I could understand. But D d(Initializer()) doesn't look like a function declaration to me. Initializer() is a (constructor) function call. It's not the right form for a parameter in a function prototype. Also, this was only one of two problems: when you replace that line with: Initializer init; D d(init); you get the wrong output. It should output i_=1. I was getting i_=0 when I posted the bug. At one point later I got i_=97. Seems like it's leaving the i_ member undefined. >// bug.cpp: In function `int main()': >// bug.cpp:35: error: request for member `i_' in `d', which is of >non-class type `D >// ()(Initializer (*)())' > >use > >D d((Initializer()));
Reopening to ...
Close as a dup of bug 9217. *** This bug has been marked as a duplicate of 9217 ***