Go Down

Topic: error in using overloaded constructor (Read 2461 times) previous topic - next topic

epokh

Hi,
I'm a new arduino developer and I'm implementing my neural net libraries for the pololu robot.
I have a weird error when trying to use a class with an overloaded constructor.
Basically the .h looks like this:
Code: [Select]

// =====================================================================================

class NeuronBP

{



 public:



   // ====================  LIFECYCLE   =========================================



   /*! Constructor f,q */

   NeuronBP(double f=DEF_F, double q=DEF_Q);

   /*! Constructor void*/

   NeuronBP();

....
}


and the c++ code looks like:

Code: [Select]

NeuronBP::NeuronBP(double f, double q)

{

 reset();

 normalize_ = true;

 burst_ = false;

 setFQ(f,q);

}


/** Constructor that sets frequency and quality.

*

*              Constructor that sets frequency and quality.

*

*      @param  f double - The frequency.

*      @param  q double - The quality.

*

*/

NeuronBP::NeuronBP()

{



 NeuronBP(NeuronBP::DEF_F,NeuronBP::DEF_Q);

}


The library is succesfully compiled and I can use it in this way:

Code: [Select]

void loop()                     // run over and over again
{

 NeuronBP nrnbp(0.2,0.5);
 for(double k=1.0;k<10.0;k=k+10.0)
 {
  nrnbp.calculate(k);
 }
}


but when I try to use:
Code: [Select]

void loop()                     // run over and over again
{

 NeuronBP nrnbp();
 for(double k=1.0;k<10.0;k=k+10.0)
 {
  nrnbp.calculate(k);
 }
}

The compiler says:
Quote

In function 'void loop()':
error: request for member 'calculate' in 'nrnbp', which is of non-class type 'NeuronBP ()()


it seems it considers two classes within the 2 different constructors.
Is it because i can't use overloading in classes?

mellis

Change:

NeuronBP nrnbp();

to:

NeuronBP nrnbp;

Putting the () after it actually makes nrnbp a pointer to a function that returns a NeuronBP, rather than just a variable of type NeuronBP.

mellis

Also, you might have problems because it's ambiguous to have both a default constructor and a constructor in which all the parameters are optional.  You should be able to just delete the default constructor (or make one of the parameters non-optional in the other constructor).

epokh

It's true: mine was a very stupid mistake!
The corrected version now looks like:
NeuronBP.h
Code: [Select]

class NeuronBP
{

 private:
   static const double  DEF_F = 0.1;
   static const double  DEF_Q = 0.51;

 public:

   // ====================  LIFECYCLE   =========================================

   /*! Constructor f,q */
   NeuronBP(double f=DEF_F, double q=DEF_Q);
...
}


and is working and compiling now I *only* have to test it  ;D

Go Up