C++访问控制public private

发布时间 2023-04-16 13:44:06作者: Z_Chan
#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;
      |               ^~~~