收到错误"invalid use of non-static data member 'stu::n' "
Getting an error "invalid use of non-static data member 'stu::n' "
#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;
}
编译器显示消息" 无效使用 非静态数据成员 'stu::n' "。 这段代码有什么问题。任何帮助都会很棒。
谢谢。
不能以这种方式使用默认参数。考虑编写两个单独的函数:
struct stu {
int n;
int add(int a, int b) { return a + b; }
int add(int a) { return a + n - 1; }
}
相关文章:
- 如何处理 c++ 中类实现中的"invalid use of non-static data member"?
- 收到错误"invalid use of non-static data member 'stu::n' "
- Centos7 g++ "to_string is not in a member of std"
- C++复制函数重载导致"must be a nonstatic member function"错误
- C++ "Invalid use of 'this' in non-member function" ,
- 当初始值设定项是基类名时'initializer does not name a non-static data member or base class'错误
- 使用 GraphicsPath::AddString() 抱怨"class 'GraphicsPath' has no member 'AddString'"
- C++线程"Call to non-static member function without an object argument"
- 出现这种错误的原因是什么"invalid use of non-static data member "
- 当我编译XG-Bosst时,出现错误"error: 'Split' is not a member of 'xgboost::common' ……"
- 当两个成员位于同一类中时出错"a nonstatic member reference must be relative to a specific object"
- 使用线程从另一个成员函数调用一个Member函数
- 为什么"non-standard syntax; use '&' to create a pointer to member"在 CTOR 中使用线程?
- 尝试打印字符串时"Python Exception <class 'gdb.error'> There is no member named _M_dataplus."
- 为什么我会收到此警告? "Member 'x' was not initialized in this constructor"
- std::p riority_queue<struct with const member&> - 'operator =' 函数不可用
- Protobuf 生成的C++类无法针对 iOS 进行编译,并显示错误"Only virtual member functions can be marked 'final'"
- 将数据添加到 CArray 会产生错误"cannot access private member declared in class 'CObject'"
- clang tidy pro type member init resharper
- 提升序列化仅适用于主要?当我在其他对象中使用时,继续说"has no member named ‘serialize’"