C++访问控制public private

#include <iostream>
class A{
    public:
        std::string head;
    private:
        std::string body;
};

int main()
{
   A a;
   a.head="888";
   a.body="999";
   return 0;
}

报错结果

main.cpp: In function ‘int main()’:
main.cpp:13:6: error: ‘std::string A::body’ is private within this context
   13 |    a.body="999";
      |      ^~~~
main.cpp:6:15: note: declared private here
    6 |   std::string body;
      |               ^~~~

 

posted @ 2023-04-16 13:37  Z_Chan  阅读(10)  评论(0编辑  收藏  举报