嵌套类,访问内部类的私人成员

发布于 2025-01-27 12:35:54 字数 2630 浏览 3 评论 0原文

我的目标是创建书籍课和书架课。这将是一个嵌套的班级,书架是外部的,书籍在内部阶级。如果我的书课中的所有数据成员都是公开的,那么我可以工作。但是,如果像我想要的那样是私有的,我由于我的print_bookshelf函数而无法工作。我会得到这些错误:

test.cpp: In member function 'void Bookshelf::print_bookshelf() const':
test.cpp:40:23: error: 'std::string Bookshelf::Book::title' is private within this context
   40 |             cout << e.title << ", " << e.author << ", " << e.pages << endl;
      |                       ^~~~~
test.cpp:16:20: note: declared private here
   16 |             string title {};
      |                    ^~~~~
test.cpp:40:42: error: 'std::string Bookshelf::Book::author' is private within this context
   40 |             cout << e.title << ", " << e.author << ", " << e.pages << endl;
      |                                          ^~~~~~
test.cpp:17:20: note: declared private here
   17 |             string author {};
      |                    ^~~~~~
test.cpp:40:62: error: 'int Bookshelf::Book::pages' is private within this context
   40 |             cout << e.title << ", " << e.author << ", " << e.pages << endl;
      |                                                              ^~~~~
test.cpp:18:17: note: declared private here
   18 |             int pages {};
      |                 ^~~~~

这是我的代码:

#include <iostream>
#include <string>
#include <vector>

using namespace std;

class Bookshelf
{
public:
Bookshelf()
{}

 class Book
    {
    private:
            string title {};
            string author {};
            int pages {};
        
        public:
            Book(string const& t, string const& a, int const p)
                : title{t}, author{a}, pages{p}
            {}

            void print() const
            {
                cout << title << ", " << author << ", " << pages << endl;
            }
        };

    void add_book(Book const& b)
    {
    bookshelf.push_back(b);
    }

    void print_bookshelf () const
    {
        for (auto e : bookshelf)
        {
            cout << e.title << ", " << e.author << ", " << e.pages << endl;
        }
    }

    private:
        vector<Book> bookshelf {};
};

int main()
{
    Bookshelf::Book book_1("Hej", "Me", 100);
    Bookshelf::Book book_2("Yo", "Me", 150);
    Bookshelf bookshelf_1;
    book_1.print();
    bookshelf_1.add_book(book_1);
    bookshelf_1.add_book(book_2);

    bookshelf_1.print_bookshelf();

    return 0;
}

My objective is to create a Book class and also a Bookshelf class. This will be a nested class where Bookshelf is the outer- and Book is in the inner class. I've got it to work if all my data members in my Book class is public. However if it is private like I want to, I don't get it to work because of my print_bookshelf function. I will get these errors:

test.cpp: In member function 'void Bookshelf::print_bookshelf() const':
test.cpp:40:23: error: 'std::string Bookshelf::Book::title' is private within this context
   40 |             cout << e.title << ", " << e.author << ", " << e.pages << endl;
      |                       ^~~~~
test.cpp:16:20: note: declared private here
   16 |             string title {};
      |                    ^~~~~
test.cpp:40:42: error: 'std::string Bookshelf::Book::author' is private within this context
   40 |             cout << e.title << ", " << e.author << ", " << e.pages << endl;
      |                                          ^~~~~~
test.cpp:17:20: note: declared private here
   17 |             string author {};
      |                    ^~~~~~
test.cpp:40:62: error: 'int Bookshelf::Book::pages' is private within this context
   40 |             cout << e.title << ", " << e.author << ", " << e.pages << endl;
      |                                                              ^~~~~
test.cpp:18:17: note: declared private here
   18 |             int pages {};
      |                 ^~~~~

This is my code:

#include <iostream>
#include <string>
#include <vector>

using namespace std;

class Bookshelf
{
public:
Bookshelf()
{}

 class Book
    {
    private:
            string title {};
            string author {};
            int pages {};
        
        public:
            Book(string const& t, string const& a, int const p)
                : title{t}, author{a}, pages{p}
            {}

            void print() const
            {
                cout << title << ", " << author << ", " << pages << endl;
            }
        };

    void add_book(Book const& b)
    {
    bookshelf.push_back(b);
    }

    void print_bookshelf () const
    {
        for (auto e : bookshelf)
        {
            cout << e.title << ", " << e.author << ", " << e.pages << endl;
        }
    }

    private:
        vector<Book> bookshelf {};
};

int main()
{
    Bookshelf::Book book_1("Hej", "Me", 100);
    Bookshelf::Book book_2("Yo", "Me", 150);
    Bookshelf bookshelf_1;
    book_1.print();
    bookshelf_1.add_book(book_1);
    bookshelf_1.add_book(book_2);

    bookshelf_1.print_bookshelf();

    return 0;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

◇流星雨 2025-02-03 12:35:54

更改此功能

void print_bookshelf () const
{
    for (auto e : bookshelf)
    {
        cout << e.title << ", " << e.author << ", " << e.pages << endl;
    }
}

只需以以下方式

void print_bookshelf () const
{
    for ( const auto &e : bookshelf )
    {
       e.print();
    }
}

,您将避免代码重复,并使用类书籍的公共接口。

Just change this function

void print_bookshelf () const
{
    for (auto e : bookshelf)
    {
        cout << e.title << ", " << e.author << ", " << e.pages << endl;
    }
}

the following way

void print_bookshelf () const
{
    for ( const auto &e : bookshelf )
    {
       e.print();
    }
}

You will avoid code duplication and use the public interface of the class Book.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文