Reputation: 1086
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??
Upvotes: 2
Views: 3374
Reputation: 372764
I believe that your problem is in this line:
manager = FileMgr(inFileName, outFileName);
From your code in FileMgr
it seems that FileMgr
has a stream input
as a data member. When you execute the above line, you'll invoke the assignment operator for FileMgr
, 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 marked private
and 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 manager
in the constructor's member initializer list:
Dictionary::Dictionary(string inFileName, string outFileName)
: manager(inFileName, outFileName) {
/* ... */
}
This will initialize manager
with the given parameters rather than trying to assign manager
an object with the right parameters.
Hope this helps!
Upvotes: 2
Reputation: 361352
Just write:
if( manager.writeFileToList(dictionary) );
&
is not needed. In fact, that causes the error!
BTW, your std::copy
is incorrectly written. Here is the correct one:
copy(istream_iterator<string>(input), istream_iterator<string>(), std::back_inserter(l));
Note the last argument. It's std::back_inserter(l)
.
Upvotes: 1
Reputation: 92854
if( manager.writeFileToList(&dictionary) )
should be changed to
if( manager.writeFileToList(dictionary) )
Note you cant convert Type*
to Type&
Upvotes: 0
Reputation: 34625
bool FileMgr::writeFileToList(list<string> &l);
FileMgr::writeFileToList
receive an argument of type list<string>
by reference.
So you should actually do -
list<string> dictionary;
if( manager.writeFileToList(&dictionary) )
^ error. You should not use & symbol here.
The argument type is not list<string>*l
to send an address.
Upvotes: 1