相关疑难解决方法(0)

错误:请求'..'中的成员'..',这是非类型的

我有一个带有两个构造函数的类,一个不带参数,另一个带一个参数.

使用带有一个参数的构造函数创建对象可以按预期工作.但是,如果我使用不带参数的构造函数创建对象,我会收到错误.

例如,如果我编译此代码(使用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;
}
Run Code Online (Sandbox Code Playgroud)

...我收到以下错误:

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 ()()’
Run Code Online (Sandbox Code Playgroud)

为什么这样,我如何使它工作?

c++

411
推荐指数
5
解决办法
41万
查看次数

标签 统计

c++ ×1