首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >获取错误"invalid use of non-static data member 'stu::n‘“

获取错误"invalid use of non-static data member 'stu::n‘“
EN

Stack Overflow用户
提问于 2020-06-27 19:21:44
回答 1查看 38关注 0票数 0
代码语言:javascript
复制
#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‘”。这段代码出了什么问题。任何帮助都是最好的。

谢谢。

EN

回答 1

Stack Overflow用户

发布于 2020-06-28 01:56:58

您不能以这种方式使用默认参数。考虑编写两个单独的函数:

代码语言:javascript
复制
struct stu {
    int n;

    int add(int a, int b) { return a + b; }
    int add(int a) { return a + n - 1; }
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/62609309

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档