С++ работа с файлами. Process finished with exit code -1073741819 (0xC0000005)
Всем привет, вообщем написал код, который сохраняет что-либо, или загружает. Сохранение/загрузка выполнены в функциях. Вроде бы все верно написал, а почему то какая-то ошибка(
вот код:
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
class Apple
{
string color;
string sort;
int age;
public:
Apple(){}
Apple(string color, string sort, int age):color(color), sort(sort), age(age){}
void Print()
{
cout<<color<<endl;
cout<<sort<<endl;
cout<<age<<endl;
}
};
template<typename T>
void save(const string& filename, const T& obj)
{
ofstream fout;
fout.open(filename);
if(fout.is_open())
{
fout.write((char*)&obj, sizeof(T));
fout.close();
}
}
template<typename T>
T load(const string& filename)
{
ifstream fin;
fin.open(filename);
T obj;
if(fin.is_open())
{
fin.read((char*)&obj, sizeof(T));
fin.close();
}
return obj; //вот тут отладчик говорит ошибка
}
int main()
{
setlocale(LC_ALL, "ru_RU.UTF-8");
cout<<"load(1) or save(2): ";
int action;
cin>>action;
if(action==1)
{
string path;
cout<<"введите путь к файлу: ";
cin>>path;
Apple a = load<Apple>(path);
a.Print();
}
else if(action==2)
{
string path;
string color;
string sort;
int age;
cout<<"Путь к фалу: ";
cin>>path;
cout<<"Цвет: ";
cin>>color;
cout<<"Сорт: ";
cin>>sort;
cout<<"Возраст: ";
cin>>age;
Apple a(color, sort, age);
save(path, a);
}
return 0;
}
Сохранения работает успешно, файл назван правильно. Загрузка выдает ошибку 