468,457 Members | 1,643 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,457 developers. It's quick & easy.

ISO C++ forbids

HiddenLayer::HiddenLayer(int numHidden, int numInput, float thresh, bool init)
{
hiddenNodes = new HiddenNeuron[numHidden](numInput, thresh, init);
numNeurons = numHidden;
};
Error:
ISO C++ forbids initialization in array new

how do i write this so i wont get an error?
id love to see your answer as a source code/
thanks in advance
Dec 11 '07 #1
7 3648
Laharl
849 Expert 512MB
It looks like you're trying to both initialize an array and/or call a constructor in the same statement. Pick one. If it's a template argument, use <> instead of [].
Dec 11 '07 #2
its not a template argument
Dec 11 '07 #3
in java i did:

public HiddenLayer(int numHidden, int numInput, double thresh, boolean init)
{
hiddenNodes = new HiddenNeuron[numHidden];
for(int i=0; i<numHidden; i++)
hiddenNodes[i] = new HiddenNeuron(numInput, thresh, init);
numNeurons = numHidden;
}
and it compiles well... however i cant do the same thing in c++
Dec 11 '07 #4
Laharl
849 Expert 512MB
That Java code looks like it would translate perfectly to C++, with changes for C++'s class syntax. I don't see where the error would come from, assuming hiddenNodes is a member variable of the class.
Dec 11 '07 #5
That Java code looks like it would translate perfectly to C++, with changes for C++'s class syntax. I don't see where the error would come from, assuming hiddenNodes is a member variable of the class.
thank you for the reply, heres a post of the entire code, i cant seem to get it to compile...
the error code is shown below,
im using Bloodshed Dev c++ ver 4.9.9.2

Expand|Select|Wrap|Line Numbers
  1. #include <vector>
  2. #include <math.h>
  3. #include <iostream>
  4. #include <stdlib.h>
  5. using namespace std;
  6.  
  7. class Neuron {
  8.  
  9. public:
  10.   Neuron();
  11.   Neuron(int axons, float threshVal, bool INIT);
  12.   Neuron(const Neuron& parent);
  13.   ~Neuron();
  14.  
  15.   // input unit functions
  16.   void setNumInputs(int val);
  17.   void setInputSet(float *inputs, int numInSet);
  18.  
  19.   // output signal functions
  20.   float generateOutputSignal(int activationFunction);
  21.   float calculateOutputSignalDerivative(int afun);
  22.  
  23.   // learning functions
  24.   void calculateWeightBiasCorrection(float learningRate);
  25.  
  26.   // set weight set values
  27.   void generateRandomInputWeights();
  28.   void setInputWeightSet(float *weights, int setSize);
  29.   void mutateInputWeights(float factor);
  30.   void updateWeightsBiases();
  31.   void increaseInputWeights(float factor);
  32.   void decreaseInputWeights(float factor);
  33.  
  34.   // bias functions
  35.   void setBias(float TVal);
  36.   void mutateBias(float factor);
  37.  
  38.   // print functions
  39.   void printWeightSet();
  40.  
  41.   int id;
  42.  
  43. // input set variables/vectors
  44.   float *neuronInputSet;
  45.   int numInputs;
  46.   float *inputWeightSet;
  47.  
  48. // threshold for firing
  49.   float bias;
  50.  
  51. // output signal value
  52.   float outputSignal;
  53.  
  54. // error correction / learning
  55. // used to calculate error term
  56.   float errorInformation;
  57.   float *weightCorrection;
  58.   float biasCorrection;
  59.   float sumWeightedInput;
  60.  
  61. // static counter
  62.   static int idCounter;
  63. };
  64. class HiddenNeuron : public Neuron
  65. {
  66. public:
  67.   HiddenNeuron(int, float, bool);
  68.   void calculateHiddenError(int afun);
  69. };
  70. //-----------------------------------
  71. class HiddenLayer
  72. {
  73. public:
  74.   HiddenNeuron *hiddenNodes;
  75.   int numNeurons;
  76.   HiddenLayer(int, int, float, bool);
  77.   ~HiddenLayer();
  78. };
  79. //-----------------------------------
  80. HiddenLayer::HiddenLayer(int numHidden, int numInput, float thresh, bool init)
  81.   //hiddenNodes = new HiddenNeuron[numHidden](numInput, thresh, init);
  82.     hiddenNodes = new HiddenNeuron[numHidden];
  83.     for(int i=0; i<numHidden; i++)
  84.           {hiddenNodes[i] = new HiddenNeuron(numInput, thresh, init);}
  85.   numNeurons = numHidden;
  86. };
  87.  
  88. HiddenLayer::~HiddenLayer()
  89.   delete [] hiddenNodes;
  90. }
  91.  
\c++\hiddenLayer.cpp In constructor `HiddenLayer::HiddenLayer(int, int, float, bool)':
83 \c++\hiddenLayer.cpp no matching function for call to `HiddenNeuron::HiddenNeuron()'
note \c++\hiddenLayer.cpp:65 candidates are: HiddenNeuron::HiddenNeuron(const HiddenNeuron&)
note \c++\hiddenLayer.cpp:65
HiddenNeuron::HiddenNeuron(int, float, bool)
85 \c++\hiddenLayer.cpp no match for 'operator=' in '*(((HiddenLayer*)this)->HiddenLayer::hiddenNodes + (+(((unsigned int)i) * 40u))) = (((HiddenNeuron*)operator new(40u)), (<anonymous>->HiddenNeuron::HiddenNeuron(numInput, thresh, (+init)), <anonymous>))'
note \c++\hiddenLayer.cpp:65 candidates are: HiddenNeuron& HiddenNeuron::operator=(const HiddenNeuron&)
C:\Documents and Settings\aresh\Desktop\Makefile.win [Build Error] [thesis/c++/hiddenLayer.o] Error 1


Expand|Select|Wrap|Line Numbers
  1. #include <vector>
  2. #include <math.h>
  3. #include <iostream>
  4. #include <stdlib.h>
  5. using namespace std;
  6.  
  7. class Neuron {
  8.  
  9. public:
  10.   Neuron();
  11.   Neuron(int axons, float threshVal, bool INIT);
  12.   Neuron(const Neuron& parent);
  13.   ~Neuron();
  14.  
  15.   // input unit functions
  16.   void setNumInputs(int val);
  17.   void setInputSet(float *inputs, int numInSet);
  18.  
  19.   // output signal functions
  20.   float generateOutputSignal(int activationFunction);
  21.   float calculateOutputSignalDerivative(int afun);
  22.  
  23.   // learning functions
  24.   void calculateWeightBiasCorrection(float learningRate);
  25.  
  26.   // set weight set values
  27.   void generateRandomInputWeights();
  28.   void setInputWeightSet(float *weights, int setSize);
  29.   void mutateInputWeights(float factor);
  30.   void updateWeightsBiases();
  31.   void increaseInputWeights(float factor);
  32.   void decreaseInputWeights(float factor);
  33.  
  34.   // bias functions
  35.   void setBias(float TVal);
  36.   void mutateBias(float factor);
  37.  
  38.   // print functions
  39.   void printWeightSet();
  40.  
  41.   int id;
  42.  
  43. // input set variables/vectors
  44.   float *neuronInputSet;
  45.   int numInputs;
  46.   float *inputWeightSet;
  47.  
  48. // threshold for firing
  49.   float bias;
  50.  
  51. // output signal value
  52.   float outputSignal;
  53.  
  54. // error correction / learning
  55. // used to calculate error term
  56.   float errorInformation;
  57.   float *weightCorrection;
  58.   float biasCorrection;
  59.   float sumWeightedInput;
  60.  
  61. // static counter
  62.   static int idCounter;
  63. };
  64. class HiddenNeuron : public Neuron
  65. {
  66. public:
  67.   HiddenNeuron(int, float, bool);
  68.   void calculateHiddenError(int afun);
  69. };
  70. //------------------------------------
  71. HiddenNeuron::HiddenNeuron(int numInputs, float threshold, bool init):Neuron(numInputs, threshold, init)
  72. { }
  73.  
  74. void HiddenNeuron::calculateHiddenError(int afun)
  75. {
  76.   float outputSignalDerivative = calculateOutputSignalDerivative(afun);
  77.   errorInformation *= outputSignalDerivative;
  78. }
  79.  
  80.  
  81.  
Dec 11 '07 #6
Laharl
849 Expert 512MB
You've certainly got some errors there.

Line 83:
This line is fine if you remove line 82. That's fine array declaration syntax, but line 82 is just a mess. It looks as though you tried to initialize the entire array with elements passing those parameters to the constructor, but that just doesn't work.

Line 85:
You're trying to reinitialize the array at each step. Use the array index operator, [], to access the elements and initialize them.
Dec 11 '07 #7
changed it to:
HiddenLayer::HiddenLayer(int numHidden, int numInput, float thresh, bool init)
{
hiddenNodes = new HiddenNeuron[numHidden];
for(int i=0; i<numHidden; i++)
{hiddenNodes[i] = HiddenNeuron(numInput, thresh, init);}
numNeurons = numHidden;
};
but i get the following error now!

this is what it calls so i dont know how to seperate both the array and initialization.....

HiddenNeuron::HiddenNeuron(int numInputs, float threshold, bool init):Neuron(numInputs, threshold, init)
{ }

ERRORS

\c++\hiddenLayer.cpp In constructor `HiddenLayer::HiddenLayer(int, int, float, bool)':

83 \c++\hiddenLayer.cpp no matching function for call to `HiddenNeuron::HiddenNeuron()'

note \c++\hiddenLayer.cpp:65 candidates are: HiddenNeuron::HiddenNeuron(const HiddenNeuron&)

note \c++\hiddenLayer.cpp:65 HiddenNeuron::HiddenNeuron(int, float, bool)
Dec 12 '07 #8

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

2 posts views Thread by Henrik S. Hansen | last post: by
5 posts views Thread by j0mbolar | last post: by
4 posts views Thread by Juhan Voolaid | last post: by
1 post views Thread by subhajit12345 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.