c++ - Error C2678: binary '<<' : no operator found which takes a left-hand operand of type 'const std::ofstream' (or there is no acceptable conversion) -
i working on mfc application , have declared ofstream object in class header, object initialized in constructor , used in other methods of same class. got following error:
error c2678: binary '<<' : no operator found takes left-hand operand of type 'const std::ofstream' (or there no acceptable conversion)
i have searched issue , found many solutions i.e. there suggestions to:
- use
#include <string> - use
#include <iostream> - use
#include <istream>
and other information got when error occur. got doesn't fix issue. kindly have @ code:
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 findstring.\n"; //trying write //other code } //destructor cgroupcombobox::~cgroupcombobox() { //other code fptr.close(); } what doing wrong here?
you declared member function qualifier const
int cgroupcombobox::findstring(int nstartafter, lpctstr lpszstring) const ^^^^^ thus this in case has type const cgroupcombobox * , may not change data member of object pointed this.
however statements
fptr<<"i findstring.\n"; //trying write requires non-const data member fptr.
so compiler issues error.
one of solutions use specifier mutable data member fptr
Comments
Post a Comment