2

I'm fairly new to C++, sorry if my questions aren't quite specific enough. Here goes.

I'm trying to overload the >> operator for a class which I have called "book." "Book" contains 'title,' 'author,' and 'publisher' string objects, a 'student count' int, and a 'price' double variable. Part of my assignment is to take these values from a provided .txt file and load values into their corresponding variables/objects. All values are on their own lines within the .txt file, and they each follow this format:

 //Title, Author, Publisher, Price

Starting Out with Java 
Gaddis  
Scott/Jones
105.99

I try to use getline() to take the string values (I use a temp string after I take the price double), but when I type it in, Visual Studio says:

Error: no intsance of overloaded function 'getline' matches the argument list.

I don't understand this. I included both <iostream> and <string>, which I believe are both required for getline to work. I'm working on getting the class file down before moving to the main code, so I apologize for not having a main code to post. Here's the .cpp file for class book:

#include <iostream>
#include <string>
#include "book.h"

using namespace std;

book::book()
{
}

book::~book()
{
}

istream& operator>> (istream &in, book &bookInfo) {
    string temp;
    getline(in, bookInfo.title);
    return in;
}

There's question number 1 down...

Assuming I can get getline to work, I have another problem. Visual Studio says that bookInfo.title is inaccessible, even though this is the accompanying .cpp file to the class. I even have the istream& function listed as a friend function in the class itself:

#include <iostream>
#include <string>

class book {
    friend istream& operator>> (istream&, book&);

public:
    book();
    virtual ~book();

private:
    string title;
    string author;
    string publisher;
    double price;
};

It should be noted that I used much the same syntax for another class, and was given no error messages.

Thanks for a very quick reply.

andre
  • 7,018
  • 4
  • 43
  • 75
Will
  • 29
  • 3
  • "Thanks for a very quick reply." I like it :) – piokuc Nov 20 '12 at 14:18
  • book.title is a private member - you can't access it outside of the book class – StevieG Nov 20 '12 at 14:20
  • @StevieG: he's declared `istream& operator>> (istream&, book&);` as a `friend`. – Cornstalks Nov 20 '12 at 14:21
  • just make the operator a member? Your second problem could be the cause of the first.. – Karthik T Nov 20 '12 at 14:22
  • Gives me an error message without the friend specification. Something about 'too many parameters' – Will Nov 20 '12 at 14:24
  • @KarthikT: it doesn't work as a member function because then the left-hand argument would be a `book`, not an `std::istream`. If you make it a member function, you can only provide one argument (the right hand argument). – Cornstalks Nov 20 '12 at 14:25

2 Answers2

3

In your header, you're not using std::. Fix that:

class book
{

    friend std::istream& operator>> (std::istream&, book&);

public:

    book();

    virtual ~book();

private:

    std::string title;
    std::string author;
    std::string publisher;
    double price;

};
Cornstalks
  • 37,137
  • 18
  • 79
  • 144
  • doesn't 'using namespace std' cover that?\ – Will Nov 20 '12 at 14:25
  • except I don't have that in there,,, – Will Nov 20 '12 at 14:27
  • @Will: Not in the header file. It does in the source file, however. If you put `using namespace std;` in your header it would cover that, but that's a *horrible* practice. Never put a global `using namespace ...;` in a header; so you have to prefix with `std::` in the header. – Cornstalks Nov 20 '12 at 14:27
  • I'll keep that in mind about horrible practice. this is for an assignment, and I guess the professor hasn't weened us off that yet. Why is it horrible exactly? – Will Nov 20 '12 at 14:30
  • @Will: Here's a few decent reads: ["using namespace" in c++ headers](http://stackoverflow.com/questions/5849457/using-namespace-in-c-headers) and [Why is 'using namespace std;' considered a bad practice in C++?](http://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-a-bad-practice-in-c). Feel free to ask more if it's not clear after reading these. – Cornstalks Nov 20 '12 at 14:33
  • @Will: Also, [a limited-scope `using namespace ...`](http://stackoverflow.com/a/1455227/1287251) is much preferred over a global `using namespace ...`. Some people still hate the limited-scope `using namespace ...`, but it's certainly less evil then a global one. – Cornstalks Nov 20 '12 at 14:35
-1

getline is a method of std::istream class, see here:

istream& getline (char* s, streamsize n );
istream& getline (char* s, streamsize n, char delim );

You should call it on class instance e.g.

your_input_stream.getline( your_params ... )
tommyk
  • 3,187
  • 7
  • 39
  • 61