I'm working at a project, and it is the first time when I work with pointers and references, and at this moment I need to set a reference to the Author
who wrote a book, as a parameter in constructor. I also want to return a constant reference to the author, using getAuthor()
method. I can't understand why I have that error (commented lines).
Book.h
class Book{
private:
std::string bookTitle;
const Author &bookAuthor;
std::string bookLanguage;
public:
Book(std::string _title, const Author &_author, std::string _language);
Book();
~Book();
Author getAuthor();
};
Book.cpp
#include "Book.h"
Book::Book(std::string _title, const Author &_author, std::string _language)
: bookTitle(_title), bookAuthor(_author), bookLanguage(_language){
}
Book::Book(){ // error C2758: 'Book::bookAuthor' : a member of reference
// type must be initialized
}
Book::~Book(){
};
Author Book::getAuthor(){
return bookAuthor;
}