I am so confused. I have a nested class where Book is the inner and Bookshelf is the outer class. When I try to create a Book object in my Bookshelf class like Book b;
it will work IF i have an empty constructor in my Book class. But when I try to use my current Book constructor that takes in 3 values, I cant declare it in my Bookshelf class. Why is that?
This is the error Im getting:
d.cpp:48:17: error: expected identifier before string constant
48 | Book b ("Hello", "2", 12);
| ^~~~~~~
d.cpp:48:17: error: expected ',' or '...' before string constant
This is my code:
#include <iostream>
#include <string>
#include <algorithm>
#include <vector>
#include <stdexcept>
using namespace std;
class Bookshelf
{
public:
Bookshelf() = default;
class Book
{
private:
string title {};
string author {};
int pages {};
int readers {};
public:
Book(string const& t, string const& a, int const p)
: title{t}, author{a}, pages{p}, readers{0}
{
}
string get_title () const
{
return title;
}
void print() const
{
cout << "Title: " << title << "\nAuthor: " << author
<< "\nPages: " << pages << "\nReaders: " << readers << endl;
}
};
void add_book(Book const& b)
{
bookshelf.push_back(b);
}
private:
vector<Book> bookshelf {};
Book b ("Hello", "2", 12);
};
int main()
{
Bookshelf::Book book_1("Hej", "Me", 100);
Bookshelf::Book book_2("Yo", "Me", 150);
Bookshelf bookshelf_1;
return 0;
}