Sign in to follow this  

Trouble filling dynamic array - C++

This topic is 4733 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I have some code that looks like this:
NeuralNetworkLayer *layers;

layers = new NeuralNetworkLayer[totalNumLayers];

layers[0] = new NeuralNetworkLayer(0, NumInputNeurons);

for(int i = 1; i < (NumberOfHiddenLayers + 1); i++)
{
	layers[i] = new NeuralNetworkLayer(1, NumInputNeurons);
}

layers[NumHiddenLayers + 1] = new NeuralNetworkLayer(2, NumberOfOutputNeurons);


and I keep getting these error message: NeuralNetworkLayer.cpp(49): error C2582: 'operator =' function is unavailable in 'Neuron' NeuralNetworkLayer.cpp(58): error C2582: 'operator =' function is unavailable in 'Neuron' NeuralNetwork.cpp(15): error C2679: binary '=' : no operator found which takes a right-hand operand of type 'NeuralNetworkLayer *' (or there is no acceptable conversion) NeuralNetwork.cpp(19): error C2679: binary '=' : no operator found which takes a right-hand operand of type 'NeuralNetworkLayer *' (or there is no acceptable conversion) NeuralNetwork.cpp(22): error C2679: binary '=' : no operator found which takes a right-hand operand of type 'NeuralNetworkLayer *' (or there is no acceptable conversion) What am I doing wrong. It should work shouldn't it? I am using VC++ 2002, with the 2003 optimizing compiler.

Share this post


Link to post
Share on other sites
I guess there is something wrong pointers or references there...but I cannot help you, as I'm a bit of a dummie using dynamic arrays...Check the definition of the "layers" variable.

[EDIT]
OOPS! Didn't see Oluseyi's post!

Share this post


Link to post
Share on other sites
Quote:
Original post by Oluseyi
You need double indirection:
NeuralNetworkLayer ** layers = new NeuralNetworkLayer * [totalLayers];
for(int i = 0; i < whateverLimitYouWant; ++i)
{
layers[i] = new NeuralNetworkLayer( ... );
}


So basically, that would make layers an array of pointers to Neurons right?

Share this post


Link to post
Share on other sites
First 2 things to remeber
1. In C++ an array and a pointer are almost the same thing

int *p;
p = new int; // p now points to an int
p = new int[3]; // p now points to an int array

2. To manipulate memory dynamically you need a pointer

int x;
int *p;
x = new int; // this is an error, x must be an int POINTER to point to dynamic memory.
p = new int; // correct

*****

NeuralNetworkLayer *layers;

Declares a pointer of type NeuralNetworkLayer, which can point to a NeuralNetworkLayer or to an array of NeuralNetworkLayers.

layers = new NeuralNetworkLayer(); // this would make it point to a single NNL
layers = new NeuralNetworkLayer[3]; // this would make it point to an array of 3 NNLs ... but NOT an array of 3 NNL pointers ...

in other words, in this scenerio ...

layers[i] is of type NeuralNetworkLayer.
so you see, now:
layers[i] = new NeuralNetworkLayer; // is an error, cause layers[i] is not a pointer as required

*****

NeuralNetworkLayer **layers;

This is a pointer to a pointer to NeuralNetworkLayer(s). It can point to a single pointer which points to a single NNL, it can point to an array of pointers which point to NNLs, or can point to a single pointer which points to an array of NNLs ... it's all in how you use it.

layers = new NeuralNetworkLayer*[3]; // this allocated an array of 3 NNL POINTERS!!! this is what you want

in this case layers[i] is of type NeuralNetworkLayer*.

so you see, now:
layers[i] = new NeuralNetworkLayer; // is not an error, cause layers[i] is a pointer as required

Share this post


Link to post
Share on other sites
Hey, Oluseyi and Xai, you even helped me understand dynamic arrays and double pointers! Good ratings to you both, guys!

Share this post


Link to post
Share on other sites

This topic is 4733 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this