-1

I'm reading in a file of user information in the format:

1 David Davidson: 64 Zoo Lane
2 Homer Simpson: 123 Fake Street, Springfield
3 Craig Boone: Presidential Suite, Lucky 38, New Vegas

I want to store the information in a class called Borrower with fields for their ID number, name, and address so I've overloaded the >> operator as follows:

istream& operator>>(istream& in, Borrower& b){
    in >> b.idNumber;
    std::getline(in, b.name, ':');
    in.ignore(1);
    in >> b.address;
    return in;
}

and I'm trying to use it in main.cpp as follows:

ifstream fileUsers;
fileUsers.open("users.txt");
if (!fileUsers.is_open()){
    exit(EXIT_FAILURE);
}
Borrower b();
while (fileUsers.good()){
    fileUsers >> b;
    cout << b;
}

but i'm getting 'ambiguous overload for operator >>' errors and 'no known conversion from 'Borrower()' to 'std::basic_istream....' and I don't know what to do, please help

Matt Pashby
  • 33
  • 1
  • 2
  • 8
  • `Borrower b()` is a function devclaration. Search for "most vexing parse", theres a ton of wuestions on it. – jrok Mar 22 '15 at 18:06
  • No, it is _not_ the most vexing parse! – Lightness Races in Orbit Mar 22 '15 at 18:06
  • Not relevant to the compiler error, but what is the type of `Borrower::address` ? If it's just a `std::string` you'll need to change your function after you fix the immediate error. – Jonathan Wakely Mar 22 '15 at 18:07
  • @LRIO sans "most" then. Yeesh! – jrok Mar 22 '15 at 18:09
  • `Borrower b()`; should simply be `Borrower b;`. Though this specific problem happens seemingly daily on SO and there are easily *hundreds* of duplicates, it is a rather difficult to hunt one down, as everyone's classes generally have different names or the question title rarely reflects the root of the problem. – WhozCraig Mar 22 '15 at 18:11
  • @jrok: It's not even that. `A a( A() )` is the most vexing parse. The language constructs involved are related, yes, but simply getting bog-standard object declaration syntax wrong is not the MVP. – Lightness Races in Orbit Mar 22 '15 at 18:11
  • @WhozCraig Perhaps there's a canonical duplicate for ambiguity resolution? If not, a meta thread should be created. –  Mar 22 '15 at 18:12
  • We do not need a canonical duplicate. This question fits into "cannot be reproduced any more" as it is a basic inability to recall proper syntax / read documentation / study a beginner book on C++. It's localised and off topic. – Lightness Races in Orbit Mar 22 '15 at 18:13
  • @LightnessRacesinOrbit Here's a [user](http://stackoverflow.com/a/29194470/3920237) calling a similar situation the "most vexing parse", linking to a post on which you succinctly commented "It is *not* the most vexing parse!" A canonical duplicate would fix this problem. –  Mar 22 '15 at 18:14
  • @remyabel: Yes, it is a _common_ misconception. That doesn't mean it's not still a misconception. :) – Lightness Races in Orbit Mar 22 '15 at 18:17

1 Answers1

3

This problem has nothing at all to do with overloading operator>>.

The mistake is that you wrote:

Borrower b();

But this is a function declaration.

Instead, write:

Borrower b;
Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
  • That's what I put before and that gives me even more errors, 'no matching function for call to 'Borrower::Borrower()' :( – Matt Pashby Mar 22 '15 at 18:11
  • 3
    @MattPashby: The correct reaction to that is to fix your class (which is missing a default constructor), not to break the declaration. – Lightness Races in Orbit Mar 22 '15 at 18:11
  • So it is. Now I'm getting all this and I don't know what it means http://imgur.com/fseqBJv This is the first time I've used c++ so I'm sorry if it's all simple mistakes – Matt Pashby Mar 22 '15 at 18:21
  • @MattPashby: Just so many problems. You need a chat room, or a colleague/friend to act as a mentor and step you through your problems one at a time. – Lightness Races in Orbit Mar 22 '15 at 19:03