I get these errors when trying to do the following.
I have a FileMgr class to handle an input and an output file with two member functions to copy each line of input into a list and to write to the output from each member of a list. note: the following functions work properly when handled directly by my main! so don’t bother trying to make out what I’m doing with the copy functions, I spent a lot of time figuring them out and now they work fine, the problem is not there.
FileMgr::FileMgr(string inFilename, string outFilename)
{
input.open(inFilename);
output.open(outFilename);
}
bool FileMgr::writeFileToList(list<string> &l)
{
// copy each line of file into new member of list<string>
if(!input.is_open())
return false;
copy(istream_iterator<string>(input), istream_iterator<string>(), back_inserter(l));
return true;
}
bool FileMgr::writeListToFile(list<string>::iterator begin, list<string>::iterator end)
{
// copy each member of list<string> in output file, beginning and ending at iterators begin, end
// note that I have to pass a "false" end iterator, that is, end--, for it to work
if(!output.is_open())
return false;
copy(begin, end, ostream_iterator<string>(output, "\n"));
return true;
}
and up to here everything is fine. then my other class, which gets the list from FileMgr, and it’s supposed to let the user edit it (im not there yet because of these errors), so heres part of my declaration:
class Dictionary
{
public:
Dictionary(string inFileName = "dictionary.txt", string outFileName = "output.txt");
void userEditor();
//private:
list<string> dictionary;
FileMgr manager;
bool findWord(string word);
bool addWord(string word);
bool deleteWord(string word);
void sortAndFix();
void saveAndExit();
and here’s my definitions so far, which is basically just the constructor:
Dictionary::Dictionary(string inFileName, string outFileName)
{
// open files and copy to list; sort and fix list.
manager = FileMgr(inFileName, outFileName);
dictionary.push_back(" ");
if( manager.writeFileToList(dictionary) )
cout << "File successfully read from " << inFileName << endl;
else
cout << "Error in reading " << inFileName << endl;
sortAndFix();
}
when I compile, I get these errors somewhere unknown in the constructor just shown (because it’s the only code in the file I get these errors from when compiling):
1>c:\program files (x86)\microsoft visual studio 10.0\vc\include\istream(860): error
C2249: 'std::basic_ios<_Elem,_Traits>::operator =' : no accessible path to private member declared in virtual base 'std::basic_ios<_Elem,_Traits>'
1> with
1> [
1> _Elem=char,
1> _Traits=std::char_traits<char>
1> ]
1> c:\program files (x86)\microsoft visual studio 10.0\vc\include\ios(177) : see declaration of 'std::basic_ios<_Elem,_Traits>::operator ='
1> with
1> [
1> _Elem=char,
1> _Traits=std::char_traits<char>
1> ]
1> This diagnostic occurred in the compiler generated function 'std::basic_istream<_Elem,_Traits> &std::basic_istream<_Elem,_Traits>::operator =(const std::basic_istream<_Elem,_Traits> &)'
1> with
1> [
1> _Elem=char,
1> _Traits=std::char_traits<char>
1> ]
1>c:\program files (x86)\microsoft visual studio 10.0\vc\include\ostream(604): error C2249: 'std::basic_ios<_Elem,_Traits>::operator =' : no accessible path to private member declared in virtual base 'std::basic_ios<_Elem,_Traits>'
1> with
1> [
1> _Elem=char,
1> _Traits=std::char_traits<char>
1> ]
1> c:\program files (x86)\microsoft visual studio 10.0\vc\include\ios(177) : see declaration of 'std::basic_ios<_Elem,_Traits>::operator ='
1> with
1> [
1> _Elem=char,
1> _Traits=std::char_traits<char>
1> ]
1> This diagnostic occurred in the compiler generated function 'std::basic_ostream<_Elem,_Traits> &std::basic_ostream<_Elem,_Traits>::operator =(const std::basic_ostream<_Elem,_Traits> &)'
1> with
1> [
1> _Elem=char,
1> _Traits=std::char_traits<char>
1> ]
I can’t understand what’s wrong. my FileMgr works fine when tested from my main, so why would the compiler trip like that when working with FileMgr from another class??
I believe that your problem is in this line:
From your code in
FileMgrit seems thatFileMgrhas a streaminputas a data member. When you execute the above line, you’ll invoke the assignment operator forFileMgr, which by default will try to copy all of the data members one at a time. However, the copy functions for streams are not accessible (they’re markedprivateand not implemented). The errors you’re getting are almost certainly due to the C++ compiler noticing that it needs to copy the streams, but failing to do so because the copy functions are inaccessible.To change this, try initializing
managerin the constructor’s member initializer list:This will initialize
managerwith the given parameters rather than trying to assignmanageran object with the right parameters.Hope this helps!