G++ compiler error - Synthesized method first requ

2019-04-27 05:45发布

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 it may have to do with how my header and source file are compiling together so here is the code for that:

Header:

#ifndef DATE_H
#define DATE_H

#include <iostream>
using namespace std;

// basic but lengthy class code

#endif

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 that the compiler is referring 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 baffled here. I have Googled around for the last hour but I can't find anything that solves my problem. Thanks for any help in advance!

2条回答
放我归山
2楼-- · 2019-04-27 06:21

The error is most likely here:

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

The function should return the stream as a reference, just like it receives it.

查看更多
时光不老,我们不散
3楼-- · 2019-04-27 06:34

The problem is that you cannot return a plain ostream. You have to return a reference to the one you received as argument (note the &).

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

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

查看更多
登录 后发表回答