title: Invalid use of 'this' in non-member function
body: <p>I had working on a class and started writing everything in the same .cpp file. However, after a while I could see the class getting bigger and bigger so I decided to split it into a .h and a .cpp file. </p>
  
  <p>gaussian.h file:</p>
  
  <pre><code>class Gaussian{
      private:
          double mean;
          double standardDeviation;
          double variance;
          double precision;
          double precisionMean;
      public:
          Gaussian(double, double);
          ~Gaussian();
          double normalizationConstant(double);
          Gaussian fromPrecisionMean(double, double);
          Gaussian operator * (Gaussian);
          double absoluteDifference (Gaussian);
  };
  </code></pre>
  
  <p>gaussian.cpp file: </p>
  
  <pre><code>#include "gaussian.h"
  #include &lt;math.h&gt;
  #include "constants.h"
  #include &lt;stdlib.h&gt;
  #include &lt;iostream&gt;
  
  Gaussian::Gaussian(double mean, double standardDeviation){
      this-&gt;mean = mean;
      this-&gt;standardDeviation = standardDeviation;
      this-&gt;variance = sqrt(standardDeviation);
      this-&gt;precision = 1.0/variance;
      this-&gt;precisionMean = precision*mean;
  } 
  
  //Code for the rest of the functions...
  
  double absoluteDifference (Gaussian aux){
      double absolute = abs(this-&gt;precisionMean - aux.precisionMean);
      double square = abs(this-&gt;precision - aux.precision);
      if (absolute &gt; square)
          return absolute;
      else
          return square;
  }
  </code></pre>
  
  <p>However, I can't get this to compile. I try running:</p>
  
  <pre><code>g++ -I. -c -w gaussian.cpp
  </code></pre>
  
  <p>But I get:</p>
  
  <pre><code>gaussian.cpp: In function ‘double absoluteDifference(Gaussian)’:
  gaussian.cpp:37:27: error: invalid use of ‘this’ in non-member function
  gaussian.h:7:16: error: ‘double Gaussian::precisionMean’ is private
  gaussian.cpp:37:53: error: within this context
  gaussian.cpp:38:25: error: invalid use of ‘this’ in non-member function
  gaussian.h:6:16: error: ‘double Gaussian::precision’ is private
  gaussian.cpp:38:47: error: within this context
  </code></pre>
  
  <p>Why can't I use this?? I am using it in the fromPrecisionMean function and that compiles. Is it because that function returns a Gaussian? Any extra explanation will be really appreciated, I am trying to learn as much as I can! Thanks! </p>