. Advertisement .
..3..
. Advertisement .
..4..
Here is the program I run:
class Foo
{
public:
Foo() {};
Foo(int a) {};
void bar() {};
};
int main()
{
// this works...
Foo foo1(1);
foo1.bar();
// this does not...
Foo foo2();
foo2.bar();
return 0;
}
After I run, it returns an error:
nonclass.cpp: In function ‘int main(int, const char**)’:
nonclass.cpp:17: error: request for member ‘bar’ in ‘foo2’, which is of non-class type ‘Foo ()()’
Does anyone have any suggestions for the problem below: request for member which is of non-class type in C++ in the cpp – How to correct it?
The cause:
This error happens due to the compiler considers
as a function declaration with the name “Foo2” and “Foo” is the return type. In this case, it is not essential to use parenthesis for instantiating a class object when you don’t want to use a parameterised constructor.
Solution:
Your error can be solved by the way removing the parenthesis from the variable declaration and write as below:
For the record.
This is not the solution for your code. However, I received the exact same error message when incorrectly accessing a method of a class instance pointed at by
myPointerToClass
, e.g.Where
It would be obvious that this is correct.