3 ms·
C++ programmers will be familiar with a related (and probably more commonplace) problem: struct Foo {int a;}; int main(int argc, char **argv) { Foo
by minimax 14y ago
C++ programmers will be familiar with a related (and probably more commonplace) problem:
struct Foo {int a;};
int main(int argc, char **argv) {
Foo foo();
foo.a = 42;
return 0;
}
Looks ok, right? Wrong.
error: request for member 'a' in 'foo', which is of non-class type 'Foo()'
- hnriot 14y agono, it doesn't look ok. You need to remove the () in your declaration. You need to take a look at how constructors work in c++
- Dylan16807 14y ago>you need to take a look I think you missed the point of the post. http://en.wikipedia.org/wiki/Most_vexing_parse http://en.wikipedia.org/wiki/Most_vexing_parse
- cube13 14y agofoo()-with the parenthesis- is a function. foo-without the parenthesis-is a variable. That means that: Foo foo(); is a function pointer declaration within the scope of the main function. Since it's a function, which does not have member types, a is not within the scope of Foo(). Now, if there was a 0 argument constructor for struct Foo, this would be valid: Foo foo = Foo();
- kelnos 14y agoFunction declaration, not function pointer declaration. The latter would be Foo (*foo)();