可能重复:
为什么使用空括号调用不带参数的构造函数是错误的?
我有小代码示例:
#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();
为什么我在堆栈上分配对象时它不起作用?