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
{
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;
}
... 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?
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.where
would obviously be correct.
I was having a similar error, it seems that the compiler misunderstand the call to the constructor without arguments. I made it work by removing the parenthesis from the variable declaration, in your code something like this:
Parenthesis is not required to instantiate a class object when you don't intend to use a parameterised constructor.
Just use Foo foo2;
It will work.
change to
You get the error because compiler thinks of
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"
.Certainly a corner case for this error, but I received it in a different situation, when attempting to overload the assignment
operator=
. It was a bit cryptic IMO (from g++ 8.1.1).I received 2 "identical" errors
(The equivalent error for
clang
is:error: member reference base type 'float' is not a structure or union
)for the lines
data.i = data;
anddata.f = data;
. Turns out the compiler was confusing local variable name 'data' and my member variabledata
. When I changed this tovoid operator=(T newData)
anddata.i = newData;
,data.f = newData;
, the error went away.Adding to the knowledge base, I got the same error for
Even though the IDE gave me the correct members for class_iter. Obviously, the problem is that
"anything"::iterator
doesn't have a member callednum
so I need to dereference it. Which doesn't work like this:...apparently. I eventually solved it with this:
I hope this helps someone who runs across this question the way I did.