#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 non-static data member 'stu::n‘”。这段代码出了什么问题。任何帮助都是最好的。
谢谢。
发布于 2020-06-28 01:56:58
您不能以这种方式使用默认参数。考虑编写两个单独的函数:
struct stu {
int n;
int add(int a, int b) { return a + b; }
int add(int a) { return a + n - 1; }
}https://stackoverflow.com/questions/62609309
复制相似问题