G ++编译器错误。什么地球上的任何这是什么意思,如何解决它? [英] G++ compiler error. What on Earth does any of this mean and how do I fix it?

查看:204
本文介绍了G ++编译器错误。什么地球上的任何这是什么意思,如何解决它?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是错误:

 在/usr/lib/gcc/x86_64-redhat-linux/4.4包含的文件中。 6 /../../../../ include / c ++ / 4.4.6 / ios:39,
来自/usr/lib/gcc/x86_64-redhat-linux/4.4.6/ .. /../../../include/c++/4.4.6/ostream:40,
来自/usr/lib/gcc/x86_64-redhat-linux/4.4.6/../../ ../../include/c++/4.4.6/iostream:40,
从date.h:15,
从date.cpp:13:
/ usr / lib / gcc /x86_64-redhat-linux/4.4.6/../../../../include/c++/4.4.6/bits/ios_base.h:在复制构造函数'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:错误:在此上下文中
/usr/lib/gcc/x86_64-redhat-linux/4.4.6/../../../../include/c++/4.4.6/iosfwd:在复制构造函数'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:合成方法'std :: basic_ios< char,std :: char_traits< char> > :: basic_ios(const std :: basic_ios< char,std :: char_traits< char>>&)'
date.cpp:function'std :: ostream operator< std :: ostream&; Date&)':
date.cpp:389:note:合成方法'std :: basic_ostream< char,std :: char_traits< char> > :: basic_ostream(const std :: basic_ostream< char,std :: char_traits< char>>&)第一项必须在这里
make:*** [date.o] Error 1

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



标题

  #ifndef DATE_H 
#define DATE_H

#include< iostream>
using namespace std;

//基本但冗长的类代码

#endif


$ b b

资料来源:

  // #include< iostream& //试过编译有没有这个,但没有改变
#include< cassert>
#include< cstdlib>
#includedate.h//这是(date.cpp:13)
//我试过使用命名空间std,只是看看会发生什么,但没有改变



最后,这里是编译器引用的函数(date.cpp:389):

  ostream运算子<<(ostream& out,const Date& date)
{
// day
out<<日期。
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;
默认值:
out<< th;
break;
}

// month
const char MONTHS [12] [10] =
{January,February,March,April ,May,June,
July,August,September,October

out<< of<< MONTHS [date.month - 1]<< ,;

// year
out<< date.year;

return out;
}

我已经在最后一个小时Google了,但我找不到任何解决我的问题。非常感谢您的帮助!

解决方案

问题是你不能返回一个简单的 code>。您必须返回对您作为参数接收的引用的引用(注意& )。

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

它不能通过在 return out上复制 out 创建一个新的 ostream code>。


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!

解决方案

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

这篇关于G ++编译器错误。什么地球上的任何这是什么意思,如何解决它?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆