I'm relativly new to C++ and this seems like a noob question but I wasn't able to solve it with other resources on the internet.
I'm trying to create a shared_ptr from a reference. I have the following Book
class:
#include <memory>
#include "Author.hpp"
class Book
{
public:
void setAuthor(const Author& t_author);
private:
std::shared_ptr<Author> m_author;
}
And this is my Author
class:
#include <memory>
class Book;
class Author
{
public:
void addBook(const Book& t_book);
private:
std::vector<std::weak_ptr<Book>> m_books;
}
I tried to implement the Book::setAuthor
method like so:
void Book::setAuthor(const Author& t_author)
{
m_author = std::shared_ptr<Author>(&t_author);
}
But if I try to compile this I get:
Invalide conversion from const Author* to Author*
Invalide conversion from sizeof to const Author
Can you please tell me what is wrong with my code? I also tried the same with the weak_ptr
but this does not work either.