私は書籍を含むbookという名前のクラスを持っています。std :: push_back中のベクタセグメンテーションエラー
#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ファイル
#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));
}
しかし、私はライブラリに新しいbook
を追加したいとき、私はmain.cppにファイル内の新しいオブジェクトのpush_back
中にセグメンテーションフォールトを取得しています
#include "book.h"
int main()
{
book* ptr;
ptr->add_book();
return 0;
}
誰かが私に問題点を説明できますか?
私はOOPで新しく、ここで多くの記事を読みましたが、解決策はまだ見つかりません。
各書籍の個別のライブラリを持っていることの全体的なアイデアは、私には少しの不良に見えます。また、 'book *'を宣言しますが、決して初期化せず、そのメソッドの1つを呼び出します。 – apokryfos
図書館に行ったり本を読んだことはありますか? – molbdnilo
私はそれがそうであることを知っていますが、それは練習のためだけです。 はい、あります –