错误:'operator<<'的声明作为非功能|

时间:2013-03-31 20:13:40

标签: c++

我很难重载<<运营商。我正在做家庭作业,我只能修改代码的某些部分。在你问之前,我坚持使用结构而不是类。以下是受影响代码的部分:

调用函数是:

/*
 * 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;
    }
}

这是.h文件:

#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

这是.cpp文件:

#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;
}

以下是我不断收到的错误消息:

错误:'运营商&lt;&lt;'的声明作为非功能|

我一直在互联网上搜索几个小时,并尝试了很多不同的方法来解决这个问题而没有成功。 请帮忙!!

我根据建议尝试了几种不同的方法来解决这个问题。以下是我尝试过的两种方法: 方法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

方法2(也未能改变错误):

#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 -------------------------------------------- ------------ 在一些评论和帮助之后,这是我目前的代码:

在.h文件中:

#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

在.cpp文件中:

#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;
}

3 个答案:

答案 0 :(得分:5)

您忘记在论据中加入<ostream>和名称空间说明符std::,这会导致您的错误。

如果您想了解下一个错误,请继续阅读:

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

这意味着您定义了一个运算符,该运算符应该作为左侧(Course)的*this的当前实例工作,因为它被定义为成员。这将导致操作员有一个左手侧和两个右手侧,这是不可能的。

您需要将运算符定义为非成员函数,因为左侧应为ostream&而不是Course&

答案 1 :(得分:2)

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

应该是

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

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

应该是

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

因为它不是Course的成员。

答案 2 :(得分:1)

std::ostream& operator << (ostream &out, const Course &c);声明中的

Course必须声明为friend,否则不能采用两个参数。

另外,第一个参数必须是std::ostream&,而不仅仅是ostream&