4

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

调用函数是:

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

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

错误:将 'operator<<' 声明为非函数|

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

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

方法二(也改失败错误):

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

重新编辑----------------------------------- --------- 经过一些评论和帮助,这是我当前的代码:

在 .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;
}
4

3 回答 3

8

您忘记在参数中包含<ostream>和命名空间说明符std::,这会导致您的错误。

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

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

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

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

于 2013-03-31T20:16:28.107 回答
3
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.

于 2013-03-31T20:15:04.580 回答
1

std::ostream& operator << (ostream &out, const Course &c);Course声明中,必须声明为friend,否则不能带两个参数。

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

于 2013-03-31T20:17:11.720 回答