LTDAN626
LTDAN626

Reputation: 61

error: declaration of 'operator<<' as non-function|

I am having a really hard time with overloading the << 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:

The calling function is:

/*
 * Write a CSV report listing each course and its enrollment.
 */
void generateReport (ostream& reportFile,
             int numCourses,
             Course* courses)
{
  for (int i = 0; i < numCourses; ++i)
    {
        //courses is an array of "Course" structs
        reportFile << courses[i] << endl;
    }
}

Here is the .h file:

#ifndef COURSE_H
#define COURSE_H

#include <iostream>
#include <string>


struct Course {
    std::string name;
    int maxEnrollment;
    int enrollment;

    Course();
    Course(std::string cName);

    std::ostream& operator << (ostream &out, const Course &c);
};

#endif

Here is the .cpp file:

#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& Course::operator << (ostream &out, const Course &c)
{
    out << c.name << "," << c.enrollment << endl;
    return out;
}

Here is the error message I keep getting:

error: declaration of 'operator<<' as non-function|

I have been searching the internet for hours and tried lots of different approaches to solve this problem with no success. Please help!!

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:

#ifndef COURSE_H
#define COURSE_H

#include <iostream>
#include <string>


struct Course {
    std::string name;
    int maxEnrollment;
    int enrollment;

    Course();
    Course(std::string cName);

};

//Moved this outside the struct
std::ostream& operator << (ostream &out, const Course &c);

#endif

Method 2 (also failed to change error):

#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& operator << (ostream &out, const Course &c);


// send course to file
ostream& Course::operator << (ostream &out, const Course &c)
{
    out << c.name << "," << c.enrollment << endl;
    return out;
}

RE-EDIT-------------------------------------------------------- After some of the comments and help, here is my current code:

In the .h file:

#ifndef COURSE_H
#define COURSE_H

#include <iostream>
#include <string>


struct Course {
    std::string name;
    int maxEnrollment;
    int enrollment;

    Course();
    Course(std::string cName);

    std::ostream& operator << (std::ostream &out, const Course &c);
};

#endif

In the .cpp file:

#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& operator << (ostream &out, const Course &c)
{
    out << c.name << "," << c.enrollment << endl;
    return out;
}

Upvotes: 6

Views: 28663

Answers (3)

user995502
user995502

Reputation:

std::ostream& operator << (std::ostream &out, const Course &c);

should be

friend std::ostream& operator << (std::ostream &out, const Course &c);

And

std::ostream& Course::operator << (std::ostream &out, const Course &c)   // Not a member of Course
{

should be

std::ostream& operator << (std::ostream &out, const Course &c)
{

Since it is not a member of Course.

Upvotes: 4

Zeta
Zeta

Reputation: 105905

You forgot to include <ostream> and the namespace specifier std:: in your argument, which leads into your error.

Read on if you want to hear about your next error:

std::ostream& operator << (std::ostream &out, const Course &c);

This means that you define an operator which should work on a current instance of a Course as left hand side (*this), since it is defined as a member. This would lead into an operator that has one left hand side and two right hand sides, which isn't possible.

You need to define the operator as a non-member function, since the left hand side should be an ostream& and not Course&.

Upvotes: 8

Emilio Garavaglia
Emilio Garavaglia

Reputation: 20739

std::ostream& operator << (ostream &out, const Course &c); inside the Course declaration, must be declared as friend, otherwise it cannot take two parameters.

also, the fist parameter must be std::ostream& and not just ostream&

Upvotes: 1

Related Questions