加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c – 错误C2678:二进制’<<':找不到运算符,它接受类

发布时间:2020-12-16 09:58:52 所属栏目:百科 来源:网络整理
导读:我正在研究MFC应用程序,并在类头中声明了一个ofstream对象,然后在构造函数中初始化该对象,并在同一类的其他方法中使用.我收到以下错误: Error C2678: binary ‘‘ : no operator found which takes a left-hand operand of type ‘const std::ofstream’ (o
我正在研究MFC应用程序,并在类头中声明了一个ofstream对象,然后在构造函数中初始化该对象,并在同一类的其他方法中使用.我收到以下错误:

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();
}

我在这做错了什么?

解决方法

您使用限定符const声明了此成员函数

int CGroupComboBox::FindString(int nStartAfter,LPCTSTR lpszString) const
                                                                    ^^^^^

因此,在这种情况下,它具有类型const CGroupComboBox *,您可能不会更改此指向的对象的数据成员.

不过这句话

fptr<<"I am FindString.n"; //Trying to write something

需要非const数据成员fptr.

所以编译器发出错误.

解决方案之一是对数据成员fptr使用说明符可变

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读