[英]member is inaccessible C++
I am new to C++.While working with class and object I tried this code:我是 C++ 的新手。在使用 class 和 object 时,我尝试了以下代码:
#include <bits/stdc++.h>
using namespace std;
class patient {
string name;
int age;
int room;
};
int main()
{
patient me;
me.name = "Zuahir";
me.age = 16;
me.room = 365;
cout << me.name;
return 0;
}
But this gives me a member inaccessible
error.但这给了我一个
member inaccessible
的错误。 Kindly help me in this case在这种情况下请帮助我
class
-members are private by default in c++. class
- 默认情况下,c++ 中的成员是私有的。 If you want to acces them directly, make them public:如果您想直接访问它们,请将它们公开:
class patient {
public:
string name;
int age = 0;//<-- don't leave those int's uninitialized ;)
int room = 0;
};
Or declare it as a struct
:或将其声明为
struct
:
struct patient {
string name;
int age = 0;
int room = 0;
};
In c++ default access modifier for a class
is private.在 c++ 中,
class
的默认访问修饰符是私有的。
Please use public access modifier before class attributes.请在 class 属性之前使用公共访问修饰符。
Like:喜欢:
class patient {
public:
string name;
int age;
int room;
};
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.