Function must have exactly one argument

前端 未结 2 1557
萌比男神i
萌比男神i 2021-01-21 15:48

I\'ve not coded in C++ for a long time and I\'m trying to fix some old code.

I\'m geting the error:

TOutputFile& TOutputFile::operator<<(TOutpu         


        
2条回答
  •  小鲜肉
    小鲜肉 (楼主)
    2021-01-21 16:23

    Operator overloaded functions which are defined as member functions accept only one parameter. In case of overloading << operator, more than one parameter is required. Making it a friend function solves this issue.

    class {
        ...
        friend TOutputFile &operator<<(TOutputFile &OutFile, const T &a);
        ...
    };
    
    template
    TOutputFile &operator<<(TOutputFile &OutFile, const T &a) {
        *OutFile.FFileID << a;
        return OutFile;
    }
    

    A function marked as friend will allow that function to access private member of the class to which it is a friend.

提交回复
热议问题