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

Invalid operands to binary expression ('ostream'(aka 'basic_ostream<char>') and 'Date')

本文关键字:ostream lt 日期 gt char basic 表达式 操作数 无效 二进制 又名      更新时间:2023-10-16

我已经阅读了这个论坛上每一个贴着这个错误标签的帖子,还没有找到一些能帮助我的东西。我得到了两个错误实例。第一个位于代码的打印列表部分。我用**做了标记。第二个是main函数。这两个都绑定到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;
 }

如果你想使用它,你需要有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成员变量都是公共的,因此不需要这样做。