Класс, свойство которого - вектор объектов класса, свойство которого - вектор double

Рейтинг: 0Ответов: 2Опубликовано: 01.03.2015

eva.cpp:

#include "stdafx.h"
#include <iostream>
#include "neuron.h"
#include "net.h"

using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{
    system("CLS"); //Очистка экрана

    neuron tmp;
    tmp.add_weight(0.444);
    net neuro_net("eva.bin");
    neuro_net.add_neuron(tmp);
    neuro_net.save();

    system("PAUSE");
}

neuron.h:

#pragma once

#include <vector>
using std::vector;

class neuron
{
private:
    vector<double> weights;
public:
    neuron();
    virtual ~neuron();
    void add_weight(double &);
    void add_weight(double);
    vector<double> get_weights();
};

neuron.cpp:

#include "stdafx.h"
#include "neuron.h"


neuron::neuron()
{
}

neuron::~neuron()
{
}

void neuron::add_weight(double & weight)
{
    weights.push_back(weight);
}

void neuron::add_weight(double weight)
{
    weights.push_back(weight);
}

vector<double> neuron::get_weights()
{
    return weights;
}

net.h

#pragma once

#include <vector>
#include <string>
#include "neuron.h"
#include <fstream>
#include <iostream>
#include <exception>

using namespace std;

class net
{
private:
    vector<neuron> hidden_layer;
    string file;
public:
    net(const string &);
    virtual ~net();
    void save();
    void add_neuron(neuron);
};

net.cpp:

#include "stdafx.h"
#include "net.h"
#include <fstream>
#include <iostream>
#include <exception>

using namespace std;

net::net(const string &path)
{
    file = path;
}

net::~net()
{

}

void net::save()
{
    try
    {
        ofstream out(file, ios::binary | ios::out);

        if (!out) 
            throw new exception("File initialization error");

        int hidden_layer_size = hidden_layer.size();
        out.write((char*)hidden_layer_size, sizeof hidden_layer_size);
        for (auto neuron : hidden_layer)
        {
            out.write((char*)neuron.get_weights().size(), sizeof neuron.get_weights().size());
            for (auto weight : neuron.get_weights())
            {
                out.write((char*)&weight, sizeof weight);
            }
        }
        out.close();
    }
    catch (exception& e)
    {
        std::cerr << e.what() << endl;
    }
}

void net::add_neuron(neuron new_neuron)
{
    hidden_layer.push_back(new_neuron);
}

В отладчике данный код валится при вызове метода net::save(), а именно строчки, где идет получение размера hidden_layer, со следующей ошибкой: alt text

Но на самом деле, полагаю, ошибка еще раньше, т.к. после добавления объекта в вектор hidden_layer в методе net::add_neuron размер hidden_layer становится неизвестен (???). Так в чем же ошибка? Что я делаю не так? Помогите разобраться.

Ответы

Ответов пока нет.