2016-10-09 6 views
-1

このエラーのラベルが付いたこのフォーラムのすべての記事を読んでいます。私はエラーの2つのインスタンスを取得します。最初のコードはコードの印刷リスト部分にあります。私は**でそれをマークしました。 2つ目はメイン関数です。これらはともに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 >>公開する場合は、' 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のメンバー変数はすべて公開されているので、その必要はありません。

もう少し高度な日付解析のためにthis answerを読んでください。

関連する問題