二进制表达式的操作数无效('ostream'(又名'basic_ostream <char>')和'Date')

时间:2016-10-09 02:54:56

标签: c++ class

我已阅读此论坛上标有此错误的每个帖子,但尚未找到可以帮助我的内容。我得到两个错误的实例。第一个位于代码的打印列表部分。我用**标记了它。第二个是主要功能。这两者都绑定到Date类。我想我需要使用指针代替。我对编程仍然很陌生,任何帮助都会受到赞赏。

#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;
 }

1 个答案:

答案 0 :(得分: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;
}

这些可以是Date命名空间中的自由函数,也可以是Date的静态成员函数。通常,后者需要使用friend关键字声明,但由于所有使用的Date成员变量都是公共的,因此不需要。

阅读this answer以获得更高级的日期解析。