ISO C++ forbids

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • saharkiz
    New Member
    • Dec 2007
    • 7

    ISO C++ forbids

    HiddenLayer::Hi ddenLayer(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
  • Laharl
    Recognized Expert Contributor
    • Sep 2007
    • 849

    #2
    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 [].

    Comment

    • saharkiz
      New Member
      • Dec 2007
      • 7

      #3
      its not a template argument

      Comment

      • saharkiz
        New Member
        • Dec 2007
        • 7

        #4
        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(nu mInput, thresh, init);
        numNeurons = numHidden;
        }
        and it compiles well... however i cant do the same thing in c++

        Comment

        • Laharl
          Recognized Expert Contributor
          • Sep 2007
          • 849

          #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.

          Comment

          • saharkiz
            New Member
            • Dec 2007
            • 7

            #6
            Originally posted by Laharl
            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

            Code:
            #include <vector>
            #include <math.h>
            #include <iostream>
            #include <stdlib.h>
            using namespace std;
            
            class Neuron {
             
            public:
              Neuron();
              Neuron(int axons, float threshVal, bool INIT);
              Neuron(const Neuron& parent);
              ~Neuron();
            
              // input unit functions
              void setNumInputs(int val);
              void setInputSet(float *inputs, int numInSet);
            
              // output signal functions
              float generateOutputSignal(int activationFunction);
              float calculateOutputSignalDerivative(int afun);
              
              // learning functions
              void calculateWeightBiasCorrection(float learningRate);
            
              // set weight set values
              void generateRandomInputWeights();
              void setInputWeightSet(float *weights, int setSize);
              void mutateInputWeights(float factor);
              void updateWeightsBiases();
              void increaseInputWeights(float factor);
              void decreaseInputWeights(float factor);
            
              // bias functions
              void setBias(float TVal);
              void mutateBias(float factor);
            
              // print functions
              void printWeightSet();
            
              int id;
            
            // input set variables/vectors
              float *neuronInputSet;
              int numInputs;
              float *inputWeightSet;
            
            // threshold for firing
              float bias;
            
            // output signal value
              float outputSignal;
            
            // error correction / learning
            // used to calculate error term
              float errorInformation;
              float *weightCorrection;
              float biasCorrection;
              float sumWeightedInput;
            
            // static counter
              static int idCounter;
            };
            class HiddenNeuron : public Neuron
            {
            public:
              HiddenNeuron(int, float, bool);
              void calculateHiddenError(int afun);
            };
            //-----------------------------------
            class HiddenLayer
            {
            public:
              HiddenNeuron *hiddenNodes;
              int numNeurons;
              HiddenLayer(int, int, float, bool);
              ~HiddenLayer();
            };
            //-----------------------------------
            HiddenLayer::HiddenLayer(int numHidden, int numInput, float thresh, bool init)
            { 
              //hiddenNodes = new HiddenNeuron[numHidden](numInput, thresh, init);
                hiddenNodes = new HiddenNeuron[numHidden];
                for(int i=0; i<numHidden; i++)
                      {hiddenNodes[i] = new HiddenNeuron(numInput, thresh, init);}
              numNeurons = numHidden;
            };
            
            HiddenLayer::~HiddenLayer()
            { 
              delete [] hiddenNodes;
            }
            \c++\hiddenLaye r.cpp In constructor `HiddenLayer::H iddenLayer(int, int, float, bool)':
            83 \c++\hiddenLaye r.cpp no matching function for call to `HiddenNeuron:: HiddenNeuron()'
            note \c++\hiddenLaye r.cpp:65 candidates are: HiddenNeuron::H iddenNeuron(con st HiddenNeuron&)
            note \c++\hiddenLaye r.cpp:65
            HiddenNeuron::H iddenNeuron(int , float, bool)
            85 \c++\hiddenLaye r.cpp no match for 'operator=' in '*(((HiddenLaye r*)this)->HiddenLayer::h iddenNodes + (+(((unsigned int)i) * 40u))) = (((HiddenNeuron *)operator new(40u)), (<anonymous>->HiddenNeuron:: HiddenNeuron(nu mInput, thresh, (+init)), <anonymous>)) '
            note \c++\hiddenLaye r.cpp:65 candidates are: HiddenNeuron& HiddenNeuron::o perator=(const HiddenNeuron&)
            C:\Documents and Settings\aresh\ Desktop\Makefil e.win [Build Error] [thesis/c++/hiddenLayer.o] Error 1


            Code:
            #include <vector>
            #include <math.h>
            #include <iostream>
            #include <stdlib.h>
            using namespace std;
            
            class Neuron {
             
            public:
              Neuron();
              Neuron(int axons, float threshVal, bool INIT);
              Neuron(const Neuron& parent);
              ~Neuron();
            
              // input unit functions
              void setNumInputs(int val);
              void setInputSet(float *inputs, int numInSet);
            
              // output signal functions
              float generateOutputSignal(int activationFunction);
              float calculateOutputSignalDerivative(int afun);
              
              // learning functions
              void calculateWeightBiasCorrection(float learningRate);
            
              // set weight set values
              void generateRandomInputWeights();
              void setInputWeightSet(float *weights, int setSize);
              void mutateInputWeights(float factor);
              void updateWeightsBiases();
              void increaseInputWeights(float factor);
              void decreaseInputWeights(float factor);
            
              // bias functions
              void setBias(float TVal);
              void mutateBias(float factor);
            
              // print functions
              void printWeightSet();
            
              int id;
            
            // input set variables/vectors
              float *neuronInputSet;
              int numInputs;
              float *inputWeightSet;
            
            // threshold for firing
              float bias;
            
            // output signal value
              float outputSignal;
            
            // error correction / learning
            // used to calculate error term
              float errorInformation;
              float *weightCorrection;
              float biasCorrection;
              float sumWeightedInput;
            
            // static counter
              static int idCounter;
            };
            class HiddenNeuron : public Neuron
            {
            public:
              HiddenNeuron(int, float, bool);
              void calculateHiddenError(int afun);
            };
            //------------------------------------
            HiddenNeuron::HiddenNeuron(int numInputs, float threshold, bool init):Neuron(numInputs, threshold, init)
            { }
            
            void HiddenNeuron::calculateHiddenError(int afun)
            {
              float outputSignalDerivative = calculateOutputSignalDerivative(afun);
              errorInformation *= outputSignalDerivative;
            }

            Comment

            • Laharl
              Recognized Expert Contributor
              • Sep 2007
              • 849

              #7
              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.

              Comment

              • saharkiz
                New Member
                • Dec 2007
                • 7

                #8
                changed it to:
                HiddenLayer::Hi ddenLayer(int numHidden, int numInput, float thresh, bool init)
                {
                hiddenNodes = new HiddenNeuron[numHidden];
                for(int i=0; i<numHidden; i++)
                {hiddenNodes[i] = HiddenNeuron(nu mInput, 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::H iddenNeuron(int numInputs, float threshold, bool init):Neuron(nu mInputs, threshold, init)
                { }

                ERRORS

                \c++\hiddenLaye r.cpp In constructor `HiddenLayer::H iddenLayer(int, int, float, bool)':

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

                note \c++\hiddenLaye r.cpp:65 candidates are: HiddenNeuron::H iddenNeuron(con st HiddenNeuron&)

                note \c++\hiddenLaye r.cpp:65 HiddenNeuron::H iddenNeuron(int , float, bool)

                Comment

                Working...