Access violation reading location

0

Proszę pomóżcie, piszę program, w zasadzie to dopiero podstawa programu. Obecnie piszę mechanizm logowania użytkownika, prostego zapisu obiektów do binarnego pliku. Przy kompilacji programu występuje błąd debuggera: Unhandled exception at 0x006AE89A (msvcr110d.dll) in money_world.exe: 0xC0000005: Access violation reading location 0x005DBEC0.
Nie wiem dlaczego się tak dzieje. Dodam że program działa poprawnie gdy nie wywołuje się metody odpowiedzialnej za zwrócenie pól klasy User. Domyślam się że problem tkwi w błędnej deklaracji obiektu User w klasie Game. Będę wdzięczny za każdą, nawet najmniejszą podpowiedź.

Zamieszczam także pliku źródłowe i nagłówkowe:

main

#include <iostream>
#include "game.h"

using namespace std;

int main(){
	system("chcp 1250");
	system("cls");

	Game* g = new Game();
	g->load_state_game();
	system("pause");
	return 0;
}

user.h

#include <iostream>
#include <string>

using namespace std;

class User{
private:
	struct age{
		int day;
		int month;
		int year;
	};
	struct user_data{
		string nick;
		string name;
		string surname;
		age a;
		string password;
		string email;
	};

	user_data u;

public:
	User();
	User(string, string, string, int, int, int, string, string);
	~User();

	user_data getData();
};

user.cpp

#include "user.h"

User::User(){}

User::User(string n, string na, string s, int d, int m , int y , string p , string e){

	u.nick = n;
	u.name = na;
	u.surname = s;
	u.a.day = d;
	u.a.month = m;
	u.a.year = y;
	u.password = p;
	u.email = e;
}

User::~User(){}

User::user_data User::getData(){
	return u;
}

game.h

#include <iostream>
#include <string>
#include <conio.h>
#include <fstream>
#include "user.h"

using namespace std;

class Game{
	private:
		
	public:
		Game();
		~Game();
		
		User* u;

		int log_in();
		int registration();
		void save_state_game();
		void load_state_game();
		void menu();
};

game.cpp

#include "game.h"

Game::Game(){}
Game::~Game(){}

int Game::log_in(){
	string login, hasło;

	cout<<"Money Maker version 1.0"<<endl;
	cout<<"-----------------------"<<endl;
	cout<<"Login: ";
	cin>>login;
	cout<<"Password: ";
	cin>>hasło;
	cout<<"-----------------------"<<endl;

	if(login==u->getData().nick)
		if(hasło==u->getData().password)
			cout<<"Dziękuję"<<endl;
		else cout<<"Błęde hasło :("<<endl;
	else cout<<"Błędny login :("<<endl;
	return 0;
}
int Game::registration(){

	string n, na, s, p, e;
	int d, m ,y; 
	bool is_good_data=false;
	int correct_answer=0;

	do{
		system("cls");

		cout<<"Registration to Money Maker"<<endl;
		cout<<"-----------------------"<<endl;
		cout<<"Nick: ";
		cin>>n;
		cout<<"Password: ";
		cin>>p;
		cout<<"Your real name: ";
		cin>>na;
		cout<<"Your real surname: ";
		cin>>s;
		cout<<"E-mail: ";
		cin>>e;
		cout<<"Day of birth: ";
		cin>>d;
		cout<<"Month of birth: ";
		cin>>m;
		cout<<"Year of birth: ";
		cin>>y;
		cout<<"-----------------------"<<endl;

		// Sprawdzanie poprawności email
		bool is_good_email=false;

		for(int i = 0; i<e.length();++i)
			if((e[i]=='@'))
				is_good_email=true;

		//-------------------------------

		if(n.length()>=5 && n.length()<=100){ // Nick
			correct_answer++;
		}
		else {cout<<"Invalid nick"<<endl;}

		if(p.length()>=5 && p.length()<=100){ // Password
			correct_answer++;
		}
		else {cout<<"Invalid password"<<endl;}

		if((e.length()>=5 && e.length()<=100) && is_good_email){ // Email
			correct_answer++;
		}
		else {cout<<"Invalid email"<<endl;}

		if(d>=1 && d<=31){ // Day
			correct_answer++;
		}
		else {cout<<"Invalid day of birth"<<endl;}

		if(m>=1 && m<=12){ // Month
			correct_answer++;
		}
		else {cout<<"Invalid month of birth"<<endl;}

		if(y>=1900 && y<=1995){ // Year
			correct_answer++;
		}
		else {cout<<"You're too young :("<<endl;}

		if(correct_answer==6)
			is_good_data=true;

		}while(!is_good_data);

		if(is_good_data){
			u = new User(n, na, s, d, m, y, p, e);
			cout<<u->getData().name<<", Your account is created"<<endl;
			save_state_game();
			cout<<"-----------------------"<<endl;
			system("pause");
		}

		return 0;
	}

void Game::save_state_game(){
	
	try{
		ofstream DATA_FILE;
		DATA_FILE.open("db.mdb", ios::out | ios::binary);

		cout<<"Size of User object is: "<<sizeof(*u)<<endl;

		DATA_FILE.write((char* )u, sizeof(*u));
		DATA_FILE.close();

		cout<<"Object u was saved succesfully"<<endl;

	}
	catch(exception X) { cout<<"Could not save :("<<endl;}
}

void Game::load_state_game(){

	u = new User();

	try{
		ifstream DATA_FILE;
		DATA_FILE.open("db.mdb", ios::in | ios::binary);

		cout<<"Size of User object is: "<<sizeof(*u)<<endl;

		DATA_FILE.read((char* )u, sizeof(*u));
		DATA_FILE.close();

		cout<<"Document loaded :)"<<endl;

		cout<<u->getData().email<<endl; // Tutaj najprawdopodobniej tkwi problem 

	}catch(exception X) {cout<<"Could not load :("<<endl;}

}
	void Game::menu(){
		int wybór;
		cout<<"Money Maker version 1.0"<<endl;
		cout<<"-----------------------"<<endl;
		cout<<"What will you do?"<<endl<<endl;
		cout<<"(1) Login"<<endl;
		cout<<"(2) Register"<<endl;
		cout<<"-----------------------"<<endl;
		cin>>wybór;

		switch(wybór){
		case 1: log_in(); break;
		case 2: registration(); break;
		default: cout<<"Invalid character"<<endl;
		}
	}

Jest to mój pierwszy większy projekt pisany obiektowo, proszę o wyrozumiałość.

0

Następuje czytanie z niezaalokowanego obszaru pamięci.
Prześledź kod debuggerem.

#Edit może problem jest w tym, że nie możesz obiektu zapisać do pliku tak, jak struktury.

0

Nie możesz tak po prostu wczytać klasy DATA_FILE.read((char* )u, sizeof(*u));
Ponieważ składowe typu string nie są cziągłymi obszarami pamięci.

1 użytkowników online, w tym zalogowanych: 0, gości: 1