嵌套类,访问内部类的私人成员
我的目标是创建书籍课和书架课。这将是一个嵌套的班级,书架是外部的,书籍在内部阶级。如果我的书课中的所有数据成员都是公开的,那么我可以工作。但是,如果像我想要的那样是私有的,我由于我的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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
更改此功能
只需以以下方式
,您将避免代码重复,并使用类书籍的公共接口。
Just change this function
the following way
You will avoid code duplication and use the public interface of the class Book.