G ++ compiler error - synthesized method required first

Here's the error:

In file included from /usr/lib/gcc/x86_64-redhat-linux/4.4.6/../../../../include/c++/4.4.6/ios:39, from /usr/lib/gcc/x86_64-redhat-linux/4.4.6/../../../../include/c++/4.4.6/ostream:40, from /usr/lib/gcc/x86_64-redhat-linux/4.4.6/../../../../include/c++/4.4.6/iostream:40, from date.h:15, from date.cpp:13: /usr/lib/gcc/x86_64-redhat-linux/4.4.6/../../../../include/c++/4.4.6/bits/ios_base.h: In copy constructor 'std::basic_ios<char, std::char_traits<char> >::basic_ios(const std::basic_ios<char, std::char_traits<char> >&)': /usr/lib/gcc/x86_64-redhat-linux/4.4.6/../../../../include/c++/4.4.6/bits/ios_base.h:790: error: 'std::ios_base::ios_base(const std::ios_base&)' is private /usr/lib/gcc/x86_64-redhat-linux/4.4.6/../../../../include/c++/4.4.6/iosfwd:47: error: within this context /usr/lib/gcc/x86_64-redhat-linux/4.4.6/../../../../include/c++/4.4.6/iosfwd: In copy constructor 'std::basic_ostream<char, std::char_traits<char> >::basic_ostream(const std::basic_ostream<char, std::char_traits<char> >&)': /usr/lib/gcc/x86_64-redhat-linux/4.4.6/../../../../include/c++/4.4.6/iosfwd:56: note: synthesized method 'std::basic_ios<char, std::char_traits<char> >::basic_ios(const std::basic_ios<char, std::char_traits<char> >&)' first required here date.cpp: In function 'std::ostream operator<<(std::ostream&, Date&)': date.cpp:389: note: synthesized method 'std::basic_ostream<char, std::char_traits<char> >::basic_ostream(const std::basic_ostream<char, std::char_traits<char> >&)' first required here make: *** [date.o] Error 1 

I think this may be due to the way my header and source file are compiled together, so here is the code for this:

Title:

 #ifndef DATE_H #define DATE_H #include <iostream> using namespace std; // basic but lengthy class code #endif 

A source:

 // #include <iostream> // tried compiling with and without this, but no change #include <cassert> #include <cstdlib> #include "date.h" // this is (date.cpp:13) // I have tried using namespace std, just to see what would happen but nothing changed 

Finally, here is the function the compiler belongs to (date.cpp: 389):

 ostream operator <<(ostream &out, const Date &date) { // day out << date.day; switch (date.day) { case 1: case 21: case 31: out << "st"; break; case 2: case 22: out << "nd"; break; case 3: case 23: out << "rd"; break; default: out << "th"; break; } // month const char MONTHS[12][10] = { "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"}; out << " of " << MONTHS[date.month - 1] << ", "; // year out << date.year; return out; } 

I am completely puzzled here. I have Googled in the last hour, but I can not find anything that solves my problem. Thanks for any help in advance!

+6
source share
2 answers

The problem is that you cannot return a simple ostream . You should return a link to the one you received as an argument (pay attention to & ).

 ostream & operator <<(ostream &out, const Date &date) 

The compiler complains that it cannot create a new ostream object by copying out in the line return out; .

+7
source

Most likely the error is here:

 ostream operator <<(ostream &out, const Date &date) 

The function should return the stream as a reference, as well as receiving it.

+2
source

Source: https://habr.com/ru/post/908330/


All Articles