简体   繁体   English

如何在本地类中定义一个朋友函数运算符>>?

[英]How to define a friend function operator>> inside a local class?

Trying to overload input operator >> inside a local class. 试图在本地类中重载输入运算符>> I tried to define friend istream &operator >> inside class Data . 我试图在class Data定义friend istream &operator >>

int readFile(char* file_name,float temperature_data[][31])
{
    class Data
    {
        public:
            int day,month,year;
            float temp;
            friend istream & operator >> (istream &in,  Data &c)
            {
                char ch;
                in >> c.day;
                in >>  ch;
                in >> c.month;
                in >>  ch;
                in >> c.year;
                in >>  ch;
                in >> c.temp;
                return in;
            }
    };
    freopen(file_name,"r",stdin);
    int i;
    Data d;
    for(i=0;;i++)
    {
        int total=0;
        char ch;
        cin>>d;
        temperature_data[d.month-1][d.day-1] = d.temp;
        ch = getchar();
        if(ch==EOF)
            break;
    }
    fclose(stdin);
    return d.year;
}

It's showing error: 显示错误:

error: can't define friend function 'operator>>' in a local class definition

Here's what I'm thinking, from my comment, which you'd call with d.read(cin) : 根据我的评论,这就是我的想法,您可以使用d.read(cin)调用它:

class Data {
public:
  int day,month,year;
  float temp;
  void read(istream& in) {
    char ch;
    in >> day;
    in >> ch;
    in >> month;
    in >> ch;
    in >> year;
    in >> ch;
    in >> temp;
  }
};

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在类中定义朋友用户定义的文字运算符 - Define friend user-defined literal operator inside the class 如何在泛型类中使用朋友函数重载运算符? - How to overload an operator with a friend function in a generic class? 为什么我们不能在本地类中定义好友函数的原因是什么? - What is the reason to we can not define friend function in local class? 如何在.cpp文件中而不是在标头中为私有类成员定义friend operator &lt;&lt;? - How to define the friend operator<< for a private class member in a .cpp file, and not in a header? "如果我们在类内部或外部定义友元函数有什么区别吗" - Is there any difference if we define friend function inside or outside of class 如何结交当地班级的功能朋友? - How to make function friend of local class? 如何使用本地类的朋友功能? - How to use friend function of local class? 如何在其声明之外定义模板类的好友模板函数? - How to define a friend template function of a template class outside of its declaration? 如何使用相同的模板定义和使用友元函数到temlate类? - How to define and use a friend function to a temlate class with the same template? 如何使用朋友函数在模板类外部重载运算符==? - how to overload operator == outside template class using friend function?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM