-1

I am trying to overload the class operator for istream (>>) and I am getting the error Ambiguous overload for operator>> for some reason. The operator for ostream works perfectly but istream does not. Does someone know why?

#include <iostream>
#include <fstream>
using namespace std;

class Person
{
    public:
      Person(string name="Empty", int num=0)
      :name(name), num(num){}

      friend istream& operator>> (istream& is, Person& o)
      {
        is >> o.name>> o.num;
         return is; 
      }

      friend ostream& operator<< (ostream& os, Person& o)
      {
        return os << o.name<< " " << o.num<< endl;
      }
    private:
      string name;
      int num;

};

int main()
{   
    ifstream fajl("input.txt");

    Person a();
    fajl >> a ;
    cout << a ;

}

input.txt:

Name1 15
Name2 16

I get the error in line: fajl >> a ;

kemis
  • 4,404
  • 6
  • 27
  • 40

1 Answers1

2

This is not a variable declaration:

Person a();

is a function declaration. The correct code to declare a variable is:

Person a;
Vittorio Romeo
  • 90,666
  • 33
  • 258
  • 416
  • 1
    A link or explanation of "*most vexing parse*" would be nice. Perhaps https://stackoverflow.com/questions/1424510/most-vexing-parse-why-doesnt-a-a-work or https://en.wikipedia.org/wiki/Most_vexing_parse – Robᵩ Jun 06 '17 at 15:47
  • This is **not** the most vexing parse. It is simply a function declaration. See the text in the link that @Robᵩ provided. – Pete Becker Jun 06 '17 at 23:52
  • 2
    The slightly vexing parse – M.M Jun 07 '17 at 08:12