Jump to content
  • Advertisement
Sign in to follow this  
Sagar_Indurkhya

Trouble filling dynamic array - C++

This topic is 4945 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 = 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
Advertisement
You need double indirection:
NeuralNetworkLayer ** layers = new NeuralNetworkLayer * [totalLayers];
for(int i = 0; i < whateverLimitYouWant; ++i)
{
layers = new NeuralNetworkLayer( ... );
}

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 = 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 is of type NeuralNetworkLayer.
so you see, now:
layers = new NeuralNetworkLayer; // is an error, cause layers 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 is of type NeuralNetworkLayer*.

so you see, now:
layers = new NeuralNetworkLayer; // is not an error, cause layers 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
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!