c++ - G++ 编译器错误 - 此处首先需要合成方法

标签 c++ compiler-errors g++ header-files

这是错误:

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

我认为这可能与我的头文件和源文件如何一起编译有关,所以这里是代码:

标题:

#ifndef DATE_H
#define DATE_H

#include <iostream>
using namespace std;

// basic but lengthy class code

#endif

来源:

// #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

最后,这是编译器引用的函数 (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;
}

我在这里完全感到困惑。我在谷歌上搜索了最后一个小时,但找不到任何解决我问题的方法。提前感谢您的帮助!

最佳答案

问题是你不能返回一个普通的ostream。您必须返回对作为参数收到的引用的引用(注意 &)。

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

编译器提示它无法通过在 return out; 行复制 out 来创建新的 ostream 对象。

关于c++ - G++ 编译器错误 - 此处首先需要合成方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9259874/

相关文章:

c++ - 原始内置类型初始化

c++ - 从函数返回二维数组

c++ - 为什么 std::move() 没有 _Remove_reference 就不能工作?

JQgrid : Change entire row's font color if one column is filled

C++:无法编译 std::tr1::unordered_map

c++ - 为什么类模板的成员函数声明都应该是良构的?

c - 如何处理错误: expected expression before ‘do’ when there is no “do” ?

c++ - "undefined reference to"使用'g++'编译C++程序

c++ - 错误 : 'std::string_view' has not been declared

c++ - 尝试制作我自己的字符串类