错误 C2678:二进制“<<”:未找到采用 'const std::ofstream' 类型的 left-hand 操作数的运算符(或没有可接受的转换)
Error C2678: binary '<<' : no operator found which takes a left-hand operand of type 'const std::ofstream' (or there is no acceptable conversion)
我正在研究 MFC Application
并在 class header 中声明了 ofstream
object,然后 object在构造函数中初始化并在相同 class 的其他方法中使用。我收到以下错误:
Error C2678: binary '<<' : no operator found which takes a left-hand operand of type 'const std::ofstream' (or there is no acceptable conversion)
我搜索了这个问题并找到了很多解决方案,即有一些建议:
- 使用
#include <string>
- 使用
#include <iostream>
- 使用
#include <istream>
我得到的一些其他信息是关于此错误何时发生的。但我得到的一切并不能解决我的问题。请看看我的代码:
CGroupComboBox.h :
private:
std::ofstream fptr;
CGroupComboBox.cpp :
//Constructor
CGroupComboBox::CGroupComboBox()
: m_dropdownListAutoWidth(true)
, m_autocomplete(true)
, m_selectionUndoByEscKey(true)
{
fptr.open("test.txt",std::ios::out); //Initialization of fptr
}
//Member Function
int CGroupComboBox::FindString(int nStartAfter, LPCTSTR lpszString) const
{
fptr<<"I am FindString.\n"; //Trying to write something
//Other Code
}
//Destructor
CGroupComboBox::~CGroupComboBox()
{
//Other Code
fptr.close();
}
我做错了什么?
因为 fptr
可以从 const
限定的函数修改,所以应该标记为 mutable
。或者,您可以在 FindString
.
中使用 const_cast
Error C2678: binary '<<' : no operator found which takes a left-hand operand of type 'const std::ofstream' (or there is no acceptable conversion)
这就是答案。 <<
尝试操作该对象,而您的对象是常量。
要解决此问题,请从 FindString 方法中删除 const
你用限定词 const 声明了这个成员函数
int CGroupComboBox::FindString(int nStartAfter, LPCTSTR lpszString) const
^^^^^
因此 this
在这种情况下具有类型 const CGroupComboBox *
并且您不能更改 this
.
指向的对象的数据成员
然而这句话
fptr<<"I am FindString.\n"; //Trying to write something
需要非常量数据成员fptr
。
所以编译器报错
解决方案之一是对数据成员 fptr
使用说明符 mutable
我正在研究 MFC Application
并在 class header 中声明了 ofstream
object,然后 object在构造函数中初始化并在相同 class 的其他方法中使用。我收到以下错误:
Error C2678: binary '<<' : no operator found which takes a left-hand operand of type 'const std::ofstream' (or there is no acceptable conversion)
我搜索了这个问题并找到了很多解决方案,即有一些建议:
- 使用
#include <string>
- 使用
#include <iostream>
- 使用
#include <istream>
我得到的一些其他信息是关于此错误何时发生的。但我得到的一切并不能解决我的问题。请看看我的代码:
CGroupComboBox.h :
private:
std::ofstream fptr;
CGroupComboBox.cpp :
//Constructor
CGroupComboBox::CGroupComboBox()
: m_dropdownListAutoWidth(true)
, m_autocomplete(true)
, m_selectionUndoByEscKey(true)
{
fptr.open("test.txt",std::ios::out); //Initialization of fptr
}
//Member Function
int CGroupComboBox::FindString(int nStartAfter, LPCTSTR lpszString) const
{
fptr<<"I am FindString.\n"; //Trying to write something
//Other Code
}
//Destructor
CGroupComboBox::~CGroupComboBox()
{
//Other Code
fptr.close();
}
我做错了什么?
因为 fptr
可以从 const
限定的函数修改,所以应该标记为 mutable
。或者,您可以在 FindString
.
const_cast
Error C2678: binary '<<' : no operator found which takes a left-hand operand of type 'const std::ofstream' (or there is no acceptable conversion)
这就是答案。 <<
尝试操作该对象,而您的对象是常量。
要解决此问题,请从 FindString 方法中删除 const
你用限定词 const 声明了这个成员函数
int CGroupComboBox::FindString(int nStartAfter, LPCTSTR lpszString) const
^^^^^
因此 this
在这种情况下具有类型 const CGroupComboBox *
并且您不能更改 this
.
然而这句话
fptr<<"I am FindString.\n"; //Trying to write something
需要非常量数据成员fptr
。
所以编译器报错
解决方案之一是对数据成员 fptr
mutable