error: request for member '..' in '..' which is of non-class type



I have a class with two constructors, one that takes no arguments and one that takes one argument.

Creating objects using the constructor that takes one argument works as expected. However, if I create objects using the constructor that takes no arguments, I get an error.

For instance, if I compile this code (using g++ 4.0.1)...

class Foo
    Foo() {};
    Foo(int a) {};
    void bar() {};

int main()
  // this works...
  Foo foo1(1);;

  // this does not...
  Foo foo2();;

  return 0;

... I get the following 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 ()()’

Why is this, and how do I make it work?

6/24/2014 5:42:00 AM

Accepted Answer

Foo foo2();

change to

Foo foo2;

You get the error because compiler thinks of

Foo foo2()

as of function declaration with name 'foo2' and the return type 'Foo'.

But in that case If we change to Foo foo2 , the compiler might show the error " call of overloaded ‘Foo()’ is ambiguous".

3/14/2016 7:50:37 PM

Just for the record..

It is actually not a solution to your code, but I had the same error message when incorrectly accessing the method of a class instance pointed to by myPointerToClass, e.g.

MyClass* myPointerToClass = new MyClass();



would obviously be correct.

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow