Will
Will

Reputation: 29

C++: getline not part of argument list

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.

Upvotes: 2

Views: 626

Answers (2)

tommyk
tommyk

Reputation: 3307

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 ... )

Upvotes: -1

Cornstalks
Cornstalks

Reputation: 38217

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;

};

Upvotes: 3

Related Questions