title: Operator overloading: calling friend function from member function
body: <p>I have a Student class. I want to overload <code>+</code> operator so I can add a double variable to class. Here is <code>Student</code> class:</p>
  
  <pre><code>class Student {
  private:
      std::string firstName;
      double grade;
  public:
      Student(const std::string &amp;firstName, double grade);
  
      double getGrade() const;
  
      friend Student operator+(double grade, const Student &amp;student);
  
      Student operator+(double grade) const;
  }; 
  </code></pre>
  
  <p>And implementation:</p>
  
  <pre><code>Student::Student(const std::string &amp;firstName, double grade) {
      this-&gt;firstName = firstName;
      this-&gt;grade = grade;
  }
  
  double Student::getGrade() const {
      return grade;
  }
  
  Student operator+(double grade, const Student &amp;student) {
      return Student(student.firstName, student.grade + grade);
  }
  
  Student Student::operator+(double grade) const {
      return operator+(grade, *this);
  }
  </code></pre>
  
  <p><code>double + Student</code> is done via friend function and <code>Student + double</code> goes through member function. When I compile I get this:</p>
  
  <pre><code>error: no matching function for call to ‘Student::operator+(double&amp;, const Student&amp;) const’
       return operator+(grade, *this);
                                    ^
  note: candidate is:
  note: Student Student::operator+(double) const
   Student Student::operator+(double grade) const {
           ^
  note:   candidate expects 1 argument, 2 provided
  </code></pre>
  
  <p>Why I can't call a friend function from a member function? </p>
  
  <p><strong>[UPDATE]</strong></p>
  
  <p>However when I overload <code>&lt;&lt;</code> operator, I can call it from member function without pre-pending <code>::</code>.</p>
  
  <pre><code>friend std::ostream &amp;operator&lt;&lt;(std::ostream &amp;os, const Student &amp;student);
  </code></pre>
  
  <p>and the implementation:</p>
  
  <pre><code>std::ostream &amp;operator&lt;&lt;(std::ostream &amp;os, const Student &amp;student) {
      os &lt;&lt; student.grade;
      return os;
  }
  </code></pre>