Mam problem z zapisaniem wektora do pliku. Kombinowalem troche z przeciazaniem operatorow ale nie wychodzilo mi to tak jakbym chcial. Czy moglby mnie ktos nakierowac co zrobic, zeby po dodaniu obiektu do wektora obiektow, obiekt ten automatycznie byl dopisywany do pliku? Na te chwile w pliku siedzi jeden obiekt, a dodawanie wiekszej ilosci juz sie nie wykonuje.
#ifndef BOOK_H
#define BOOK_H
#include<string>
#include<vector>
#include<iostream>
#include<fstream>
class book
{
public:
std::string Author, Title;
int Year;
book(){}
book(std::string author, std::string title, int year);
friend struct library;
friend std::ostream& operator<<(std::ostream& out, book const& book);
~book(){}
};
struct library
{
public:
friend class book;
std::vector<book> books;
void add_book(const book& value);
};
#endif // BOOK_H
#include "book.h"
book::book(std::string author, std::string title, int year)
:Author(author), Title(title), Year(year){}
std::ostream& operator<<(std::ostream& out, book const& book)
{
return out<<book.Author<<book.Title<<book.Year;
}
void library::add_book(const book& value)
{
std::fstream file;
file.open("data.txt", std::ios::app);
books.push_back(value);
file<<value;
file.close();
}
new
i zostawiania wycieku pamięci? Pokazanie złych praktyk?