0

So I currently trying to make the output stream insertion operator and trying to make it so it displays the date a certain way but I keep getting the "Attempting to reference a deleted function" error. I am not sure what the issue could be. Any help?

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

This is the operator that I am trying to get to work.

 friend ostream operator << (ostream& output, Date& obj) { output << wordmonth[month - 1] << " " << day << ". " << year; return output; } }; 
5
  • Can you explain what wordmonth, referenced by your overloaded << operator, you think should be? It can't be the wordmonth class member, of course, because, as you specified this is a friend function, and not a class member. Only class members can access other class members. Commented Feb 9, 2020 at 14:33
  • 1) Post the entire error log, not just some fragment of a single message. In C++, errors are usually chained. 2) Learn How to Ask and post a minimal reproducible example. Commented Feb 9, 2020 at 14:37
  • @SamVarshavchik wordmonth is an array of all the months spelled out, because in the program sometimes the month has to be the number of the month and sometimes it needs to the word. And I made it a friend function because without it it says that there are too many parameters. Commented Feb 9, 2020 at 14:40
  • @rustyxSeverity 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 Commented Feb 9, 2020 at 14:41
  • Unrelated: you are declaring all the class fields as static. Why? I think you don't want that. Commented Feb 9, 2020 at 15:58

1 Answer 1

1

From the comments:

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

This error basically says "you can't copy a stream" (the deleted function is the copy-constructor of basic_ostream, the base class template of ostream).

That's because ostream operator << (ostream& output, Date& obj) is declared as returning ostream by-value (i.e. a copy).

Change that to ostream& operator << (ostream& output, Date& obj) to return it by-reference.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.