Image

Imagetimv wrote in Imagecpp

This is what I ended up doing...it compiles and runs, but is it correct? hehe. (Well, I think it is.)

I believe my error in the code that I posted before was as follows...

Neuron* neurons; //this is a pointer to a memory location
//...
neurons = new Neuron[nNeurons]; //new returns a memory location
//...
//Then, when I did this...
neuron[i] = Neuron(nInputs);
//...it created a Neuron that went outta scope after the Layer ctor

//What I wanted to do was...
neuron[i] = *new Neuron(nInputs);



class Layer
{

public:

Layer();
Layer(int,int);
~Layer();

void setInputs(double[]);
double getOutput(int);

private:

int nInputs;

int nNeurons;
Neuron* neurons;

};

Layer::Layer(){}

Layer::Layer(int nInputs, int nNeurons)
{
this->nInputs = nInputs;
this->nNeurons = nNeurons;
neurons = new Neuron[nNeurons];
for (int i=0; i<nNeurons; i++) { neurons[i] = *new Neuron(nInputs); } } Layer::~Layer() { delete[] neurons; } void Layer::setInputs(double inputs[]) { for (int i=0; i<nNeurons; i++) neurons[i].setInputs(inputs); } double Layer::getOutput(int n) { return neurons[n].getOutput(); }