Heath Martens Heath Martens - 1 month ago 18
C++ Question

Yet another error: invalid operands to binary expression ('ostream'(aka 'basic_ostream<char>') and 'Date')

I have read through every post on this forum labeled by this error and have yet to find something that would help me. I get two instances of the error. The first one is located in the print list portion of the code. I have marked it with **. The second one is n the main function. Both of these are tied together to to Date class. I think I need to use a pointer instead. I'm still very new to programming and any help would be apresciated.

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

Answer

You need to have istream& operator>>(istream&, Date&) if you want to use it.

For example:

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

The same goes for ostream& operator<<(ostream&, Date const&):

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

These can be either free functions in Date's namespace or static member functions of Date. Normally, the latter would need to be declared with the friend keyword, but since all used Date's member variables are public, there's no need for that.

Read this answer for a little more advanced date parsing.

Comments