错误:请求成员'..'在'..'是非类类型 [英] 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.
例如,如果我编译这个代码(使用g ++ 4.0.1)。 ..
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?
推荐答案
Foo foo2();
更改为
Foo foo2;
您得到错误,因为编译器认为
You get the error because compiler thinks of
Foo foo2()
'foo2'和返回类型'Foo'。
as of function declaration with name 'foo2' and the return type 'Foo'.
但在这种情况下如果我们改成 Foo foo2
编译器可能会显示错误重载的调用'Foo()'是不明确的
。
But in that case If we change to Foo foo2
, the compiler might show the error " call of overloaded ‘Foo()’ is ambiguous"
.
这篇关于错误:请求成员'..'在'..'是非类类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!