I have a class named book, that contains books.
#ifndef BOOK_H
#include<string>
#include<vector>
#include<iostream>
#define BOOK_H
class book
{
public:
std::string Author, Title;
int Year;
book(){}
book(std::string author, std::string title, int year);
~book(){}
void add_book();
std::vector<book*>library;
};
#endif
book.cpp file
#include "book.h"
book::book(std::string author, std::string title, int year)
:Author(author), Title(title), Year(year){}
void book::add_book()
{
int y;
std::string a, t;
std::cin>>a;
std::cin>>t;
std::cin>>y;
library.push_back(new book(a, t, y));
}
But when I want to add a new book to the library, I'm getting a segmentation fault during the push_back of the new object in main.cpp file
#include "book.h"
int main()
{
book* ptr;
ptr->add_book();
return 0;
}
Could someone explain to me what the problem is?
I'm new in OOP, and though I've read a lot of posts here I still can't find a solution.
book*but never initialize it and then call one of its methods.