【发布时间】:2020-06-27 11:21:44
【问题描述】:
#include <bits/stdc++.h>
using namespace std;
struct stu {
int n;
stu(int _n = 0):n(_n) { }
int add(int a, int b = n-1) {
return a + b;
}
};
int main() {
stu obj = stu(5);
cout << obj.add(10) << endl;
}
编译器显示消息“invalid use of 非静态数据成员'stu::n'"。 这段代码有什么问题。任何帮助都会很棒。
谢谢。
【问题讨论】:
-
int b = n-1是无效的默认参数,如documentation 中所述。