类运算符的不明确重载>>

Ambiguous overload for class operator>>

本文关键字:gt 重载 不明确 运算符      更新时间:2023-10-16

我试图超载istream(>>(的类操作员,并且由于某种原因,我正在收到错误 Ambiguous overload for operator>>。Ostream的操作员运作完美,但Istream却没有。有人知道为什么吗?

#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

我在排队中获得错误:fajl >> a ;

这不是变量声明:

Person a();

函数声明。声明变量的正确代码是:

Person a;