2012-02-13 16 views
6

Dưới đây là các lỗi:G ++ lỗi - Phương pháp tổng hợp đầu tiên yêu cầu ở đây

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 

tôi nghĩ rằng nó có thể phải làm với cách tiêu đề của tôi và tập tin nguồn được biên dịch lại với nhau vì vậy đây là mã cho rằng:

Tiêu đề:

#ifndef DATE_H 
#define DATE_H 

#include <iostream> 
using namespace std; 

// basic but lengthy class code 

#endif 

Nguồn:

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

Cuối cùng, đây là chức năng mà các trình biên dịch được đề cập đến (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; 
} 

Tôi hoàn toàn bối rối ở đây. Tôi có Googled xung quanh cho giờ cuối cùng nhưng tôi không thể tìm thấy bất cứ điều gì mà giải quyết vấn đề của tôi. Nhờ sự giúp đỡ trước!

+4

Xin lưu ý rằng không nên có 'sử dụng không gian tên std;' trong tiêu đề. –

+0

Xem câu hỏi này để giải thích lý do tại sao ... http://stackoverflow.com/questions/14575799/using-namespace-std-in-a-header-file – Mawg

Trả lời

7

Sự cố là bạn không thể trả lại số đơn giản ostream. Bạn phải trả lại một tham chiếu đến đối số mà bạn đã nhận làm đối số (lưu ý số &).

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

Trình biên dịch phàn nàn rằng nó không thể tạo ra một đối tượng mới bằng cách sao chép ostreamout trên dòng return out;.

2

Lỗi này là có khả năng nhất ở đây:

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

Các chức năng nên trả lại dòng suối như một tài liệu tham khảo, giống như nó nhận được nó.

Các vấn đề liên quan