I have started to learn C++ some weeks ago and now I have a problem with my current project.

I wanted to write templates, that allow me to save objects into binary datas and load them again later. Here ist the code of these templates:

```
#ifndef TOOLS_H
#define TOOLS_H

#include <fstream>
#include <string>

namespace Tools
{
    template <class T>
    void writeBinaryFile(std::string filename, T object)
    {
        std::ofstream of(filename, std::ios::out | std::ios::binary);
        of.write((char*) &object, sizeof(T));
        of.close();
    }

    template <class P>
    P readBinaryFile(std::string filename)
    {
        P temp;
        std::ifstream ifs(filename, std::ios::in | std::ios::binary);
        ifs.read((char*) &temp, sizeof(P));
        ifs.close();
        return temp;
    }
}

#endif

```

I created a class called GameSettings. The header data is:

```
#ifndef GAMESETTINGS_H
#define GAMESETTINGS_H

#include <iostream>
#include <string>
#include "SFML\Graphics.hpp"

class GameSettings
{
public:
    GameSettings();
    GameSettings(std::string playerName, bool sound, int volume, int level);
    ~GameSettings();

    void setPlayerName(std::string playername){ playerName = playername; };
    void setSound(bool sound){ this->sound = sound; };
    void setVolume(int volume){ this->volume = volume; };
    void setLevel(int level){ this->level = level; };

    const std::string getPlayerName() { return playerName; }
    const bool getSound() { return sound; }
    const int getVolume() { return volume; }
    const int getLevel() { return level; }

private:
    std::string playerName;
    bool sound;
    int volume;
    int level;
};

#endif

```

With the cpp-data:

```
#include "GameSettings.h"

GameSettings::GameSettings(std::string playerName, bool sound, int volume, int level)
{
    this->playerName = playerName;
    this->sound = sound;
    this->volume = volume;
    this->level = level;
}

GameSettings::GameSettings():
playerName(""),
sound(true),
volume(0),
level(0)
{
}

GameSettings::~GameSettings()
{
}

```

When I start the main-function:

```
#include <iostream>
#include "GameSettings.h"
#include "Tools.h"

    int main()
    {
        GameSettings* gs = new GameSettings("Andrew", true, 100, 3);
        Tools::writeBinaryFile<GameSettings>("gamesettings.bin", *gs);

        gs->setPlayerName("TEST");

        *gs = Tools::readBinaryFile<GameSettings>("gamesettings.bin");

        std::cout << gs->getPlayerName();
        std::getchar();

        return 0;
    }

```

an error occurs:

Unhandled exception at 0x5a1cad54 (msvcp100d.dll) in Mohrhuhn.exe: 0xC0000005: Access violation writing location 0xfeeefeee.

Can somebody help me?

Sincerly, Andrew