简体   繁体   中英

Nested classes, access private members in inner class

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM