可能重複:
Why is it an error to use an empty set of brackets to call a constructor with no arguments?C++繼承錯誤
我有小的代碼示例:
#include <iostream>
using namespace std;
class A
{
public:
void print()
{
cout << "Hello" << endl;
}
};
class B: public A
{
public:
B() { cout << "Creating B" << endl;}
};
int main()
{
B b();
b.print(); // error: request for member ‘print’ in ‘b’, which is of non-class type ‘B()()’
}
但是如果我改變到下面有一個,如果有效的話,
B* b = new B();
b->print();
爲什麼當我在堆棧上分配對象時不工作?