2016-10-09 3 views
-1

이 오류로 분류 된이 포럼의 모든 게시물을 읽었으며 아직 도움이되지 않는 내용을 찾아야합니다. 오류가 두 번 발생합니다. 첫 번째 것은 코드의 인쇄 목록 부분에 있습니다. **로 표시했습니다. 두 번째는 n 주요 기능입니다. 이것들은 모두 Date 클래스에 묶여 있습니다. 대신 포인터를 사용해야한다고 생각합니다. 나는 여전히 프로그래밍에 익숙하지 않으며 도움이 될 것입니다.이진 표현식 ('ostream'(일명 'basic_ostream <char>') 및 '날짜')에 대한 피연산자가 올바르지 않습니다.

#include <iostream> 
#include <fstream> 
#include <string> 

using namespace std; 

//------------------------------------------------------------------- 

class Date                //Class Date 
{ 
public: 
    int day; 
    int month; 
    int year; 
    Date(); 
    Date(int,int,int); 
    ~Date(void); 
}; 

Date::Date(void) 
{ 
    day = 0; 
    month = 0; 
    year = 0; 
} 

Date::Date(int month, int day, int year) 
{ 
    day = day; 
    month = month; 
    year = year; 
}                   //Class Date 

//--------------------------------------------------------------------------------------------- 
                     //Class Book 
class Book 
{ 
public: 
    string _title; 
    string _author; 
    Date _published; 
    string _publisher; 
    float _price; 
    string _isbn; 
    int _page; 
    int _copies; 
    Book(); 
    Book(string,string,Date,string,float,string,int,int); 
    ~Book(void); 
}; 

Book::Book(void) 
{ 
    _title = ""; 
    _author = ""; 
    //_published; 
    _publisher = ""; 
    _price = 0; 
    _isbn = ""; 
    _page = 0; 
    _copies = 0; 

} 

Book::Book(string title, string author, Date published, string publisher, float price, string isbn, int page, int copies) 
{ 
    _title = title; 
    _author = author; 
    _published = published; 
    _publisher = publisher; 
    _price = price; 
    _isbn = isbn; 
    _page = page; 
    _copies = copies; 
}                   //Class Book 

//--------------------------------------------------------------------------------------------- 

class Node                 //Class Node 
{ 
    friend class LinkedList; 
private: 
    Book *_book; 
    Node *_next; 
public: 
    Node(void); 
    Node(Book*); 
    Node(Book*,Node*); 
    ~Node(void); 
}; 

Node::Node(void) 
{ 
    _book = NULL; 
    _next = NULL; 
} 

Node::Node(Book *book) 
{ 
    _book = book; 
    _next = NULL; 
} 

Node::Node(Book *book, Node *next) 
{ 
    _book = book; 
    _next = next; 
}                  //Class Node 

//--------------------------------------------------------------------------------------------- 

class LinkedList              //Class LinkedList 
{ 
private: 
    Node *_head; 
    Node *_tail; 
public: 
    LinkedList(void); 
    LinkedList(Book*); 
    ~LinkedList(void); 
    void insert_front(Book*); 
    void insert_rear(Book*); 
    void print_list(void); 
}; 

LinkedList::LinkedList(void) 
{ 
    _head = NULL; 
    _tail = NULL; 
} 


LinkedList::LinkedList(Book *book) 
{ 
    _head = new Node(book); 
    _tail = _head; 
}                  //Class LinkedList 

//--------------------------------------------------------------------------------------------- 

void LinkedList::insert_front(Book *book) 
{ 
    if(_head == NULL) 
    { 
     _head = new Node(book); 
     _tail = _head; 
    } 
    else 
     _head = new Node(book, _head); 
} 

void LinkedList::insert_rear(Book *book) 
{ 
    if(_head == NULL) 
    { 
     _head = new Node(book); 
     _tail = _head; 
    } 
    else 
    { 
     _tail -> _next = new Node(book); 
     _tail = _tail -> _next; 
    } 
} 

void LinkedList::print_list(void) 
{ 
    Node *temp = _head; 
    while(temp!= NULL) 
    { 
     cout << temp -> _book -> _title << endl; 
     cout << temp -> _book -> _author << endl; 
     **cout << temp -> _book -> _published << endl;** 
     cout << temp -> _book -> _publisher << endl; 
     cout << temp -> _book -> _price << endl; 
     cout << temp -> _book -> _isbn << endl; 
     cout << temp -> _book -> _page << endl; 
     cout << temp -> _book -> _copies << endl; 
     temp = temp -> _next; 
     cout << endl; 
    } 
} 

LinkedList::~LinkedList(void) 
{ 

} 

Book::~Book(void) 
{ 

} 

Date::~Date(void) 
{ 

} 

Node::~Node(void) 
{ 

} 
//--------------------------------------------------------------------------------------------- 
                //Main 
int main(void) 
{ 
    LinkedList myList; 
    ifstream myFile("input.txt"); 

    string title; 
    string author; 
    Date published; 
    string publisher; 
    float price; 
    string isbn; 
    int page; 
    int copies; 

    while(myFile) 
    { 
     getline(myFile,title); 
     getline(myFile,author); 
     **cin >> published;** 
     getline(myFile,publisher); 
     cin >> price; 
     getline(myFile,isbn); 
     cin >> page; 
     cin >> copies; 

     myList.insert_front(new  Book(title,author,published,publisher,price,isbn,page,copies)); 
    } 

    myList.print_list(); 

    return 0; 
} 
+0

'cin >> published;을 원한다면'Date'에'operator >>'를 제공해야합니다. – songyuanyao

답변

2

사용하려면 istream& operator>>(istream&, Date&)이 필요합니다. 예를 들어

:

istream& operator>>(istream& s, Date& d) { 
    return s >> d.year >> d.month >> d.day; 
} 

같은이 ostream& operator<<(ostream&, Date const&) 간다 :

ostream& operator<<(ostream& s, Date const& d) { 
    return s << d.year << d.month << d.day; 
} 

이 될 수 있습니다 DateDate의 네임 스페이스 또는 정적 멤버 함수의 무료 기능. 일반적으로 후자는 friend 키워드로 선언해야하지만, Date의 멤버 변수는 모두 public이므로 사용할 필요가 없습니다.

좀 더 고급 날짜 구문 분석을 위해 this answer을 읽으십시오.

관련 문제