istream& operator ostream& operator help

I am trying to call the operator << from the Book program to implement the operator >> in the Warehouse program. However, I am confused to how I am supposed to call this operator. Any help will be appreciated. Thank You!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
  #include "Warehouse.h"
#include "Book.h"
#include <iostream>
#include<string>

using namespace std;

static const int MAX_BOOKS = 35;

clss Warehouse {
 	/**
 	* @param is the input stream
	* @param warehouse the warehouse object reference
	* @return the input stream
	*/
 	friend istream& operator >> (istream& is, Warehouse& warehouse);

 	/**
 	* @param os the output stream
 	* @param warehouse the warehouse object reference
 	* @return the output stream
 	*/
 	friend ostream& operator << (ostream& os, const Warehouse& warehouse);
 	public:
 		static const int MAX_BOOKS = 35;
 		Warehouse();
 		/**
		 * @param isbn the ISBN number to search for
 		 * @param book reference to the matched book object, if found
 		 * @return true if found.
		 */
 		bool find (string isbn, Book& book) const;
 		/**
 		 * Prints the inventory of the Warehouse (i.e. list all the books)
		 */
		void list () const;
 	private: /* extra credit */
 		void sort_();
 	private:
 		Book books[Warehouse::MAX_BOOKS];
		int bookCount;
};

#endif /* WAREHOUSE_H */

istream& operator >> (istream& is, Warehouse& warehouse){
    bookCount = 0;
    for (int i=0; i<MAX_BOOKS;i++){


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#include "Book.h"
#include <iostream>
#include <string>
using namespace std;


static const int MAX_BOOKS = 35;
static const int MAX_AUTHORS = 20;
string title_;
string authors_[Book::MAX_AUTHORS];
int authorCount_;
string publisher_;
short yearPublish_;
bool hardcover_;
float price_;
string isbn_;
long copies_;


istream& operator >> (istream& is, Book& book){
    is >> book.title_;
    is.ignore();
    is >> book.authorCount_;
    for (int j=0;j<book.authorCount_;j++){
        is >> book.authors_[j];
        is.ignore();
    }
    is >> book.publisher_;
    is.ignore();
    is >> book.yearPublish_;
    is.ignore();
    is >> book.hardcover_;
    is.ignore();
    is >> book.price_;
    is.ignore();
    is >> isbn_;
    is.ignore();
    is >> copies_;
    is.ignore();
    return is;
}
ostream& operator << (ostream& os, const Book& book){
    os<< book.title_ << "\n";
    for (int j=0; j<book.authorCount_; j++){
        os<< book.authors_[j] << "n";
    }
    os<< book.publisher_ << "n";
    os<< book.yearPublish_;
    os<<book.hardcover_ << "n";
    os<< book.price_ << "n";
    os<< book.isbn_ << "n";
    os<< book.copies_ << "n";
    return os;
}
Hello daveed,

The short anser is that yo do not call your over loaded "operator <<" or "operator >>" you just use them as in:
1
2
3
4
5
6
7
8
9
10
11
int main()
{
	ifstream inFile;
	// code to open file

	Wharhouse warehouse;

	infile >> warehouse;

	// rest of code
}

On line 8 the compiler will pick up on the difference and use your overloaded operator because "warehouse" is a object of the class and not a regular variable.

It would also be helpful if you post the whole code because I see problems in what you have posted. With out the rest of the code it is not easy to tell what is all wrong.

In your first bit of code line 1 includes "Warehouse.h", but how can you include this file before you define the class which comes later.

You include "Book.h", but the contentsare unavailable to see what you did.

Line 8 defines "MAX_BOOKS", but you define it again on line 25. You do not needit twice and it would be better defined in "Book.h" and not "Warehouse.h".

The header files "iostream" and "string" should not be needed in a header file if done correctly.

The line using namespace std; should never be in a header file. This is worth reading: http://www.lonecpluspluscoder.com/2012/09/22/i-dont-want-to-see-another-using-namespace-xxx-in-a-header-file-ever-again/

In the second bit of code "Book.h" tends to work better when it comes after "iostream" and "string".

Since you have forward declared the "<<" and ">>" in "Warehouse.h" you should include this file in the ".cpp" file.

Line 7 was defined in "warehouse.h" and probably in "Book.h" so you do not need it here again. I do not believe the "static" qualifier is needed. I have defined constant variables in header files and have not needed to use "static".

Lines 9 - 17 are global variables and should be avoided. They are better defined inside a function that uses them.

"double" is the preferred floating point type as a "float" is less precise.

I do not know why you have ended your variable names with an underscore, but this form is best left to the include files that come with the compiler. Not to say there is anything wrong with it, but it is not necessary.

This is some of what I see right now compared to what I have done and what I have seen others do.

Hope that helps,

Andy
Thank you so much for the help!

I was wondering if using ">>" for the warehouse class will mess anything up because there is a separate operator >> that is defined in the Warehouse Class.
First, how many threads do you need on one topic? Earlier threads:
http://www.cplusplus.com/forum/beginner/251525/
http://www.cplusplus.com/forum/beginner/251543/

Note:
1
2
3
class Warehouse {
  friend istream& operator >> ( istream&, Warehouse& );
};

The istream& operator >> ( istream&, Warehouse& ); is not "defined in the Warehouse Class".

That operator is a standalone function that takes two parameters, (two operands). You merely declare that the function that is not a member of the class nevertheless has access to private members of the class. A friend.


You could have written more explicitly/redundantly:
1
2
3
4
5
6
7
class Warehouse; // forward declaration of a class typename

istream& operator >> ( istream&, Warehouse& ); // declare function opX

class Warehouse {
  friend istream& operator >> ( istream&, Warehouse& ); // give opX additional privileges
};



1
2
3
ifstream inFile;
Warehouse warehouse;
infile >> warehouse;

On line 3 the compiler looks at all* the operator>> overloads that it knows of and picks the one that matches the types of inFile and warehouse (or reports an error).
Among them are also:
1
2
operator >> ( istream&, Book& ) // #1
operator >> ( istream&, Warehouse& ) // #2 

Second operand, warehouse, has type Warehouse that is an exact match for #2.
The inFile has type ifstream that IS-A istream via public inheritance.
A Warehouse is not a Book.
Hence #2 is a clear match and #1 is not. No ambiguity.



* The "all" is a simplification; the real lookup rules are more complex.
Topic archived. No new replies allowed.