我是一个新手 C++ 程序员,在编写代码时遇到了这个错误:
C:\Users\Matt\Desktop\C++ Projects\OperatorOverload\students.h|8|error: non-static reference member 'std::ostream& Student::out', can't use default assignment operator|
错误出现在此头文件的第 8 行:
#ifndef STUDENTS_H
#define STUDENTS_H
#include <string>
#include <vector>
#include <fstream>
class Student {
private:
std::string name;
int credits;
std::ostream& out;
public:
// Create a student with the indicated name, currently registered for
// no courses and 0 credits
Student (std::string studentName);
// get basic info about this student
std::string getName() const;
int getCredits() const;
// Indicate that this student has registered for a course
// worth this number of credits
void addCredits (int numCredits);
void studentPrint(std::ostream& out) const;
};
inline
std::ostream& operator<< ( std::ostream& out, const Student& b)
{
b.studentPrint(out);
return out;
}
bool operator== ( Student n1, const Student& n2)
{
if((n1.getCredits() == n2.getCredits())&&(n1.getName() == n2.getName()))
{
return true;
}
return false;
}
bool operator< (Student n1, const Student& n2)
{
if(n1.getCredits() < n2.getCredits()&&(n1.getName() < n2.getName()))
{
return true;
}
return false;
}
#endif
问题是我不太确定错误的含义,也不知道如何修复它。有没有人有可能的解决方案?