不断"Attempting to reference a deleted function"

Keep getting "Attempting to reference a deleted function"

本文关键字:deleted function to Attempting 不断 reference      更新时间:2023-10-16

所以我目前正在尝试制作输出流插入运算符并尝试使其以某种方式显示日期,但我不断收到"尝试引用已删除的函数"错误。我不确定问题可能是什么。有什么帮助吗?

class Date {
private:
static int day;
static int month;
static int year;
public:
static string wordmonth[];
Date(int d, int m, int y) {
this->day = d;
month = m;
year = y;
}
Date(int d) {
this->day = d;
}
Date() {
}
void setDay(int d) {
this->day = d;
}
void setMonth(int m) {
month = m;
}
void setYear(int y) {
year = y;
}
int getDay() {
return day;
}
int getMonth() {
return month;
}
int getYear() {
return year;
}
void print1();
void print2();
void print3();
Date operator ++ () {
day++;
return *this;
}
Date operator ++ (int) {
Date day = *this;
++* this;
return day;
}
Date operator -- () {
day--;
return day;
}
Date operator -- (int) {
--day;
return day;
}
Date operator - (Date v) {
Date temp;
temp.day = day - v.day;
temp.month = month;
temp.year = year;
return temp;
};  

这是我正在尝试工作的操作员。

friend ostream operator << (ostream& output, Date& obj) {
output << wordmonth[month - 1] << " " << day << ". " << year;
return output;
}
};

从评论:

Error C2280 'std::basic_ostream<char,std::char_traits<char>>::basic_ostream(const std::basic_ostream<char,std::char_traits<char>> &)': attempting to reference a deleted function

这个错误基本上是说">你不能复制流"(删除的函数是basic_ostream的复制构造函数,ostream的基类模板(。

这是因为ostream operator << (ostream& output, Date& obj)被声明为返回ostream按值(即副本(。

将其更改为ostream& operator << (ostream& output, Date& obj)以按引用返回它。