简体   繁体   中英

Error C2679 binary '<<': no operator found which takes a right-hand operand of type 'T'

I try to compile the following code:

class CFileOperations
{
    ...
    template <typename T>
    inline void load_and_save_data(std::fstream* stream, T& value, const EOperation operation)
    {
        switch (operation) {
            case EOperation::OpSave:
                *stream << value;     <-- here
                break;
            case EOperation::OpLoad:
                *stream >> value;     <-- and here
                break;
        }
    }
    ...
};

I get the following errors:

Error   C2679   binary '<<': no operator found which takes a right-hand operand of type 'T' (or there is no acceptable conversion)
Error   C2679   binary '>>': no operator found which takes a right-hand operand of type 'T' (or there is no acceptable conversion)

For example, I use it this way, with number being an 'int':

this->load_and_save_data(stream, number, operation);

I'm using Visual C++ 2019.

What's the root cause, and how to solve it. Any idea?

My bad, one of the calls was with a 'class enum'. Of course, >> and << are not defined for it.

For @cdhowie, here are two examples of the resulting simplicity (with the help of load_and_save_data template methods):

Here mMembers is a std::unorderedmap (cf. save_and_load_data in the question above, I have also one for the starndard containers):

void CHexArea::load_and_save()
{
    this->load_and_save_data((char&)mColor);
    this->load_and_save_data(mTouchLeft);
    this->load_and_save_data(mTouchRight);
    this->load_and_save_data(mTouchBottom);
    this->load_and_save_data(mTouchTop);

    this->load_and_save_data(mMembers);
}

Here, in preferences, there are two versions of files:

void CHexPreferences::load_and_save()
{
   if( this->is_loading() ) {
      this->reset(); // version's forward compatibility
   }

   int version = 2;
   this->load_and_save_data(version);
   this->load_and_save_data(mBoardOrientation);
   this->load_and_save_data(mBoardSize);
   this->load_and_save_data(mComputerStarts);
   this->load_and_save_data(mComputerInitialTurns);
   if( version >= 2) {
      this->load_and_save_data(mComputerTilesPerTurn);
   }
   this->load_and_save_data(mDebugFlags);
}

Simple and clear.

Of course, there are two methods ( load() and save() ) that are the outer interface and calls those here above, but: 1. They are part of a library (no need to rewrite them, OO as usual) and 2. The core of the load/save is written only once in load_save_data, with the advantage of simplicity, and having corresponding load and save code (types, order...).

Of course, there are cons, but I hope you'll see that it may make sense for some people to think that there are (IMHO very strong) pros as well.

The rest is a matter of taste.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM