より明確に言うと、() を使用してオブジェクトを作成するときにインスタンス変数にアクセスしようとすると、コンパイル時エラーが発生しますが、アクセスしないと、コードはコンパイルされ、期待どおりに実行されます。また、この問題はデフォルト コンストラクターにのみ適用されます。その理由を理解したいと思います。
using namespace std;
#include <iostream>
class Student {
public:
int gpa;
Student() {
gpa = 4;
}
Student( int x ) {
gpa = x;
}
};
int main() {
Student zero;
Student sally( 2 );
Student jack();
cout << zero.gpa << endl; //prints 4
cout << sally.gpa << endl; // prints 2
cout << jack.gpa << endl; //error: request for member 'gpa' in 'jack', which is of non-class type 'Student()'
}