簡體   English   中英

成員不可訪問 C++

[英]member is inaccessible C++

我是 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;
}

但這給了我一個member inaccessible的錯誤。 在這種情況下請幫助我

class - 默認情況下,c++ 中的成員是私有的。 如果您想直接訪問它們,請將它們公開:

class patient {
  public:
    string name;
    int age = 0;//<-- don't leave those int's uninitialized ;)
    int room = 0;
};

或將其聲明為struct

struct patient {
    string name;
    int age = 0;
    int room = 0;
};

在 c++ 中, class的默認訪問修飾符是私有的。

請在 class 屬性之前使用公共訪問修飾符。

喜歡:

class patient {
  public:
   string name;
   int age;
   int room;
};

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM