我有一個帶有兩個構造函數的類,一個不接受任何參數,一個接受一個參數。錯誤:請求會員'..'在'..'中是非班級類型
使用帶有一個參數的構造函數創建對象將按預期方式工作。但是,如果我使用不帶參數的構造函數創建對象,則會出現錯誤。
舉例來說,如果我編譯這段代碼(使用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;
}
...我收到以下錯誤:
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()()’
這是爲什麼,和我如何使它工作?
相關:http://stackoverflow.com/q/2318650/69537 – Meysam 2012-08-06 06:24:35