I have a course with 2 constructors, one that takes no arguments and one the takes one argument.

You are watching: Request for member which is of non-class type

Creating objects making use of the constructor the takes one dispute works together expected. However, if I produce objects utilizing the constructor that takes no arguments, I acquire an error.

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

class Foo public: Foo() ; Foo(int a) ; void bar() ;;int main() // this works... Foo foo1(1); foo1.bar(); // this go not... Foo foo2(); foo2.bar(); return 0;... I get the adhering to error:

nonclass.cpp: In role ‘int main(int, const char**)’:nonclass.cpp:17: error: request for member ‘bar’ in ‘foo2’, which is the non-class type ‘Foo ()()’Why is this, and also how execute I do it work?



Foo foo2();change to

Foo foo2;You get the error because compiler think of

Foo foo2()as of function declaration through name "foo2" and also the return kind "Foo".

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



Just because that the record..

It is actually no a systems to your code, yet I had actually the same error blog post when erroneously accessing the an approach of a course instance pointed to by myPointerToClass, e.g.

MyClass* myPointerToClass = new MyClass();myPointerToClass.aMethodOfThatClass();where

myPointerToClass->aMethodOfThatClass();would obviously it is in correct.


Adding come the understanding base, I got the very same error because that

if(class_iter->num == *int_iter)Even despite the concept gave me the correct members for class_iter. Obviously, the difficulty is that "anything"::iterator doesn"t have a member dubbed num therefore I need to dereference it. I beg your pardon doesn"t occupational like this:

if(*class_iter->num == *int_iter)...apparently. I eventually solved it with this:

if((*class_iter)->num == *int_iter)I expect this help someone who runs throughout this inquiry the way I did.

Parenthesis is not compelled to instantiate a course object as soon as you don"t intend to use a parameterised constructor.

Just use Foo foo2;

It will work.

I was having actually a similar error, it seems that the compiler misunderstand the call to the constructor there is no arguments. I made it job-related by removed the parenthesis from the variable declaration, in your code something prefer this:

class Foo public: Foo() ; Foo(int a) ; void bar() ;;int main() // this works... Foo foo1(1); foo1.bar(); // this walk not... Foo foo2; // there is no "()" foo2.bar(); return 0;
I ran into a case where I got that error message and had

Foo foo(Bar());and was usually trying to happen in a temporary Bar thing to the Foo constructor. Transforms out the compiler to be translating this to

Foo foo(Bar(*)());that is, a duty declaration whose surname is foo the returns a Foo that takes in an discussion -- a function pointer returning a Bar through 0 arguments. When passing in temporaries favor this, much better to usage Bar rather of Bar() to eliminate ambiguity.

If you desire to declare a brand-new substance with no parameter (knowing the the object have actually default parameters) don"t compose

form substance1();but

form substance;
Certainly a corner case for this error, however I received it in a various situation, when attempting to overload the assignment operator=. It to be a bit cryptic IMO (from g++ 8.1.1).

See more: How To Get The Huckleberry Destiny 2, The Huckleberry

#include enum DataType DT_INT32, DT_FLOAT;struct PrimitiveData union MyData int32_t i; rise f; data; enum DataType dt; layout void operator=(T data) switch(dt) situation DT_INT32: data.i = data; break; case DT_FLOAT: data.f = data; break; default: break; ;int main() struct PrimitiveData pd; pd.dt = DT_FLOAT; pd = 3.4f; return 0;I obtained 2 "identical" errors

error: inquiry for member ‘i’ in ‘data’, which is the non-class type ‘float’(The equivalent error for clang is:error: member reference base type "float" is not a structure or union)

for the currently data.i = data; and data.f = data;. Transforms out the compiler was confusing regional variable name "data" and my member variable data. Once I adjusted this come void operator=(T newData) and also data.i = newData;, data.f = newData;, the error go away.