Layer::Layer(int LayerSize, Layer PrevLayer){
Size=LayerSize;
Column=(Neuron*)malloc(Size);
for(int i=0;i<Size;i++){
Column[i]=Neuron(LayerSize,LayerSize,LayerSize);
Input[i]=&Column[i].Input; //1
PrevLayer.Output[i]=Input[i]; //2
}
我試圖讓Input[i]
指向相應神經元的輸入。雖然內存地址似乎是正確的,但當我嘗試分配行//1
上的成員變量Input的地址值時,程序崩潰。任何想法有什麼不對,或者我可以使用的更好的方法?無法將成員變量地址分配給指針
下面是有關成員的類
}
class Neuron{
public:
Neuron(int PrevColumnSize, int ThisColumnSize, int NextColumnSize);
//Constructor: Generates a Neuron with random values
double Input; //input of each neuron
};
class Layer{
private:
int Size; //Number of Neurons in the layer
public:
Layer(int LayerSize); //Constructor; Layer with no attached layers; used at the start of a network
Layer(int LayerSize, Layer PrevLayer);
//Constructor; Layer which attaches itself to the next, and the previous layers; unused
Neuron* Column; //Column of Neurons
double** Input; //Inputs to Neurons
};
可能重複[是未初始化的局部變量最快的隨機數生成器?](https://stackoverflow.com/questions/31739792/is-uninitialized-local-variable-the-fastest-random-number-generator) – LogicStuff
Off topic:'Layer PrevLayer'是按值傳遞的。對PrevLayer進行的任何更改都會複製到一個副本中,並且當PrevLayer超出範圍並被銷燬時,該副本將在該函數結束時丟失。這將是一個非常好的時間來熟悉[什麼是三條法則?](https://stackoverflow.com/questions/4172722/what-is-the-rule-of-ree) – user4581301
感謝user4581301,我我也嘗試將它定義爲'layer * PrevLayer',並將它傳遞給一個地址,但這也不起作用 – lordflashhart