|
|
 |
| Author |
Message |
Krank

|
Posted: Visual C++ Language, Strange error C2228 |
Top |
Hello, I am facing a very strange error with this code: Basically, I have a wrapper object, and I want to initialize it with a temporarily created object (which is perfectly legal). But depending on the declaration, the wrapper object creation fails.
Here is a sample code:
struct a
{ int s; explicit a(int s_):s(s_){} };
class a_wrapper { a m_a; public: explicit a_wrapper(const a& a_) :m_a(a_){}
int get(){return m_a.s;} };
int main() { int nb_act=30;
a_wrapper s1(a(nb_act));//seen as function definition s1.get(); //error C2228: left of '.begin' must have class/struct/union //type is 'overloaded-function'
a_wrapper s2(a(30)); //seen as object creation s2.get(); //no error!!
return 0; }
For me, the line "a_wrapper s1(a(nb_act));" is seen as a function definition, but I don't understand why... VC 7.1 and 8 generate the same thing.
Any idea
Thank you very much,
Charles Brossollet
Visual C++6
|
| |
|
| |
 |
Viorel.

|
Posted: Visual C++ Language, Strange error C2228 |
Top |
Since the compiler allows parenthesis for parameters -- e.g. "void f(int (x));" -- then s1 is interpreted as a local prototype for "a_wrapper s1(a nb_act)" function.
To achieve the desired meaning, try this:
a const tmp(nb_act);
a_wrapper s1(tmp);
s1.get();
or this:
a_wrapper s1((a)nb_act);
s1.get();
I hope this makes sense.
|
| |
|
| |
 |
einaros

|
Posted: Visual C++ Language, Strange error C2228 |
Top |
Viorel. wrote: | |
Since the compiler allows parenthesis for parameters -- e.g. "void f(int (x));" -- then s1 is interpreted as a local prototype for "a_wrapper s1(a nb_act)" function.
|
|
That's what Scott Meyers describes as "C++'s most vexing parse" in Effective STL. A must-read IHMO 
|
| |
|
| |
 |
einaros

|
Posted: Visual C++ Language, Strange error C2228 |
Top |
By the way, "a_wrapper s1( (a(nb_act)) );" should also fix the problem -- added parenthesis, that is.
|
| |
|
| |
 |
Krank

|
Posted: Visual C++ Language, Strange error C2228 |
Top |
einaros wrote: | | By the way, "a_wrapper s1( (a(nb_act)) );" should also fix the problem -- added parenthesis, that is. |
|
You're right, it compiled. Thanks to you all for your help!
Cheers, Charles
|
| |
|
| |
 |
| |
|