title: error: declaration of 'operator<<' as non-function|
body: <p>I am having a really hard time with overloading the &lt;&lt; operator.  I am working on a homework assignment where I can only modify certain portions of the code.  Before you ask, I AM stuck using a struct instead of a class.  Here are the portions of the affected code:</p>
  
  <p>The calling function is:</p>
  
  <pre><code>/*
   * Write a CSV report listing each course and its enrollment.
   */
  void generateReport (ostream&amp; reportFile,
               int numCourses,
               Course* courses)
  {
    for (int i = 0; i &lt; numCourses; ++i)
      {
          //courses is an array of "Course" structs
          reportFile &lt;&lt; courses[i] &lt;&lt; endl;
      }
  }
  </code></pre>
  
  <p>Here is the .h file:</p>
  
  <pre><code>#ifndef COURSE_H
  #define COURSE_H
  
  #include &lt;iostream&gt;
  #include &lt;string&gt;
  
  
  struct Course {
      std::string name;
      int maxEnrollment;
      int enrollment;
  
      Course();
      Course(std::string cName);
  
      std::ostream&amp; operator &lt;&lt; (ostream &amp;out, const Course &amp;c);
  };
  
  #endif
  </code></pre>
  
  <p>Here is the .cpp file:</p>
  
  <pre><code>#include "course.h"
  
  using namespace std;
  
  
  Course::Course()
  {
      name = "";
      maxEnrollment = 0;
      enrollment = 0;
  }
  
  Course::Course(string cName)
  {
      name = cName;
      maxEnrollment = 0;
      enrollment = 0;
  }
  
  // send course to file
  ostream&amp; Course::operator &lt;&lt; (ostream &amp;out, const Course &amp;c)
  {
      out &lt;&lt; c.name &lt;&lt; "," &lt;&lt; c.enrollment &lt;&lt; endl;
      return out;
  }
  </code></pre>
  
  <p>Here is the error message I keep getting:</p>
  
  <p>error: declaration of 'operator&lt;&lt;' as non-function|</p>
  
  <p>I have been searching the internet for hours and tried lots of different approaches to solve this problem with no success.
  Please help!!</p>
  
  <p>I tried a couple of different methods to fix this based on advice.  Here are two methods I tried which did not work:
  Method 1:</p>
  
  <pre><code>#ifndef COURSE_H
  #define COURSE_H
  
  #include &lt;iostream&gt;
  #include &lt;string&gt;
  
  
  struct Course {
      std::string name;
      int maxEnrollment;
      int enrollment;
  
      Course();
      Course(std::string cName);
  
  };
  
  //Moved this outside the struct
  std::ostream&amp; operator &lt;&lt; (ostream &amp;out, const Course &amp;c);
  
  #endif
  </code></pre>
  
  <p>Method 2 (also failed to change error):</p>
  
  <pre><code>#include "course.h"
  
  using namespace std;
  
  
  Course::Course()
  {
      name = "";
      maxEnrollment = 0;
      enrollment = 0;
  }
  
  Course::Course(string cName)
  {
      name = cName;
      maxEnrollment = 0;
      enrollment = 0;
  }
  
  std::ostream&amp; operator &lt;&lt; (ostream &amp;out, const Course &amp;c);
  
  
  // send course to file
  ostream&amp; Course::operator &lt;&lt; (ostream &amp;out, const Course &amp;c)
  {
      out &lt;&lt; c.name &lt;&lt; "," &lt;&lt; c.enrollment &lt;&lt; endl;
      return out;
  }
  </code></pre>
  
  <p>RE-EDIT--------------------------------------------------------
  After some of the comments and help, here is my current code:</p>
  
  <p>In the .h file:</p>
  
  <pre><code>#ifndef COURSE_H
  #define COURSE_H
  
  #include &lt;iostream&gt;
  #include &lt;string&gt;
  
  
  struct Course {
      std::string name;
      int maxEnrollment;
      int enrollment;
  
      Course();
      Course(std::string cName);
  
      std::ostream&amp; operator &lt;&lt; (std::ostream &amp;out, const Course &amp;c);
  };
  
  #endif
  </code></pre>
  
  <p>In the .cpp file:</p>
  
  <pre><code>#include "course.h"
  
  using namespace std;
  
  Course::Course()
  {
      name = "";
      maxEnrollment = 0;
      enrollment = 0;
  }
  
  Course::Course(string cName)
  {
      name = cName;
      maxEnrollment = 0;
      enrollment = 0;
  }
  
  
  // send course to file
  ostream&amp; operator &lt;&lt; (ostream &amp;out, const Course &amp;c)
  {
      out &lt;&lt; c.name &lt;&lt; "," &lt;&lt; c.enrollment &lt;&lt; endl;
      return out;
  }
  </code></pre>