title: C++: error: ‘class’ has no member named
body: <p>For my graduation thesis I'm programming some finite element code, or to be more precise, I'm modifying an existing program, which is based on 2 class libraries provided by my faculty. As such, I cannot modify these classes, because those are meant to be general.</p>
  
  <p>I have created a class <code>BurgersMSrc</code>, which inherits from a parent class <code>ValSrc</code>. The child class I have expanded with a method <code>calcFourierCoefficient</code>. During compile time I get the following error:</p>
  
  <pre><code>burgers1d.cpp:268:12: error: ‘class ValSrc’ has no member named ‘calcFourierCoefficient’
  </code></pre>
  
  <p>This makes sense, since the variable is defined as:</p>
  
  <pre><code>ValSrc* srcTerm;
  </code></pre>
  
  <p>which does not have the method defined. Later on the variable is instantiated as either</p>
  
  <pre><code>srcTerm = new ConstVS(f);
  </code></pre>
  
  <p>or</p>
  
  <pre><code>srcTerm = new BurgersMSrc(prm);
  </code></pre>
  
  <p>where the instantiation is depending on the problem type. Conditionally defining <code>srcTerm</code> as either <code>ConstVS</code> or <code>BurgersMSrc</code> object yields:</p>
  
  <pre><code>error: ‘srcTerm’ was not declared in this scope
  </code></pre>
  
  <p>That is not an option either. </p>
  
  <p>So in the end my question is: </p>
  
  <blockquote>
    <p>If a variable is defined as a parent class, but instantiated as a child class, how do I access the child's method(s)?</p>
  </blockquote>
  
  <p>Any help is really appreciated and thanks in advance for your answers.</p>
  
  <p><strong>Edit</strong></p>
  
  <p>FYI, I'm not very experienced in C++, yet I do have some programming experience in C# and VBA. I do however like to learn, so pointers towards the right direction are more than welcome :)</p>
  
  <p><strong>/Edit</strong></p>
  
  <p>The relevant lines from the header file:</p>
  
  <pre><code>#ifndef BurgersMSS_H
  #define BurgersMSS_H
  #include "mfem.hpp"
  #include "mex.h"
  
  class BurgersMSol: public ValSrc
  {
     ...
  };
  
  class BurgersMSrc: public ValSrc
  {
  
  public:
  
      typedef ValSrc Super;
  
      BurgersMSrc(ParamDB &amp;prm) {init(prm);}
      virtual void init(ParamDB &amp;prm);
  
      ~BurgersMSrc(){}
  
      inline void getValues  (Vector &amp;coords, Vector &amp;msrc){}
      void calcFourierCoefficient(int p){}
  
  private:
  
      double   nu;
      double   Tn;
      int prob;
      int nTimeSteps;
      int specModes;
      double s_n;
      double tT;
      double deltaT;
      vector &lt;double&gt; a_re;
      vector &lt;double&gt; a_im;
      int accuracy;
      double randomNr;
  
      double randomNumber(int p){return randomNr;}
  
  };
  
  #endif
  </code></pre>
  
  <p>The relevant lines from the CPP-file:</p>
  
  <pre><code>#include "BurgersMSS.h"
  
  void BurgersMSol::init(ParamDB &amp;prm) 
  { 
      ...
  }
  
  BurgersMSol::~BurgersMSol(){}
  
  inline void BurgersMSol::getValues (Vector &amp;coords, Vector &amp;msol) 
  {
      ...
  }
  
  BurgersMSrc::init(ParamDB &amp;prm) 
  { 
      Super::init(); objectName="BurgersMSrc";
      nu = 1.0; prm.find("nu", nu);
      prob = 1; prm.find("problem", prob);
      if (prob == 3)
      {
          ...
          this-&gt;calcFourierCoefficient(accuracy);
      }
  }
  
  BurgersMSrc::~BurgersMSrc(){}
  
  inline void BurgersMSrc::getValues  (Vector &amp;coords, Vector &amp;msrc)
  {
      ...
  }
  
  void BurgersMSrc::calcFourierCoefficient(int p)
  {   
      for(int n=0;n&lt;specModes;n++)
      {
          if (time == 0)
          {
              a_re[n] = randomNumber(p);
              a_im[n] = randomNumber(p);
          }
          else
          {
              a_re[n] = a_re[n]*exp(-tT) + randomNumber(p);
              a_im[n] = a_im[n]*exp(-tT) + randomNumber(p);
          }
      }
  }
  
  double BurgersMSrc::randomNumber(int p)
  {
      int mod = pow(10,p);    
      int rN = -mod + rand() % (2*mod);
      randomNr = rN/(double)mod;
      return randomNr;
  }
  </code></pre>
  
  <p>The relevant lines from the main program:</p>
  
  <pre><code>#include "mfem.hpp"
  #include "mex.h"
  #include "BurgersMSS.h"
  ...
  
  int main (int argc, char *argv[]) {
      ...
  
      ValSrc *srcTerm;
      ...
  
      if (problem==1) {
          ... srcTerm = new ConstVS(f);
          ...
      } else if (problem==2) {
          ... srcTerm  = new ConstVS(f);
          ...
      } else if (problem==3){
          srcTerm = new BurgersMSrc(prm); 
          ...
      } else {
          srcTerm = new BurgersMSrc(prm); 
          ...
      }
  
      ...
      stiffInt-&gt;setSrc(*srcTerm);
      ...
  
      for (int step = 0; step &lt; nTimeSteps; step ++) {
      ...
          if (problem == 3)
          {
              srcTerm-&gt;calcFourierCoefficient(accuracy); //This line throws the error
          }
          ...
      }
  
      ...
      return 0;
  
  }
  </code></pre>