错误 C2679 二进制"<<":未找到采用类型为 'T' 的右侧操作数的运算符
Error C2679 binary '<<': no operator found which takes a right-hand operand of type 'T'
我尝试编译以下代码:
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;
}
}
...
};
我收到以下错误:
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)
例如,我以这种方式使用它,数字是"int":
this->load_and_save_data(stream, number, operation);
我正在使用Visual C++ 2019。
根本原因是什么,以及如何解决它。知道吗?
我的错,其中一个调用是使用"类枚举"。当然,>>和<<不是为此定义的。
对于@cdhowie,下面是两个简单性的示例(借助load_and_save_data模板方法(:
这里mMembers是一个std::unorderedmap(参见上面问题中的save_and_load_data,我也有一个用于starndard容器(:
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);
}
在这里,在首选项中,有两个版本的文件:
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);
}
简单明了。
当然,有两个方法(load((和save(((是外部接口,并调用上面的那些,但是:1. 它们是库的一部分(无需重写它们,像往常一样 OO(和 2.加载/保存的核心在load_save_data中只编写一次,优点是简单,并具有相应的加载和保存代码(类型、顺序......
当然,也有缺点,但我希望你能看到,有些人认为也有(恕我直言非常强大(优点可能是有道理的。
剩下的就是品味问题了。
相关文章:
- 如何防止clang格式在流运算符调用之间添加换行符<<
- 如何显式调用运算符<<
- 为什么COUT在朋友函数中不起作用,该功能超载了操作员&lt;&lt;这是一个iStream运算符
- C++运算符<<调用::ostream而不是std::osttream
- BOOST ::变体无法解决运算符&lt;&lt;对于STD :: Ostream
- 过载输出<<用于类的运算符,以打印其中的元组
- C++ostream:没有运算符匹配<<&应在'&'代币
- 重载运算符<<:此运算符函数的参数太多
- C++继承运算符<<
- 重载运算符<<用于模板类.即使使用好友关键字也无法获得私人会员
- 如何过载<<用于YAML::Emitter的运算符,以序列化包含另一个自定义类的向量的自定义类
- 为什么字符串流运算符<<擦除原始值
- 关于使用运算符<<为新手提供C++中的模板
- 我已经完成了<<运算符重载,但它'It’不起作用
- 重载运算符<<输出地址而不是数据成员
- 错误:没有匹配'运算符<<"在'std::cout
- 重载运算符<<用于ostream语法
- 当运算符<存在时,为什么要定义 LT?
- log4cxx访问异常,使用<<运算符和宏
- 重载<<运算符错误C2804:二进制'运算符<<'参数太多