奇怪的iostream编译错误

时间:2011-03-19 07:09:51

标签: c++ compiler-errors iostream

尝试执行以下操作时出现这些错误。 我有一个FileMgr类来处理输入和一个带有两个成员函数的输出文件,用于将每行输入复制到一个列表中,并写入列表中每个成员的输出。注意:当我的主要直接处理时,以下功能正常工作!所以不要试图弄清楚我正在做什么与复制功能,我花了很多时间搞清楚它们现在它们工作正常,问题不存在。

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

到此为止一切都很好。然后我的另一个类,从FileMgr获取列表,并且它应该让用户编辑它(因为这些错误,我还没有),所以这是我声明的一部分:

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

这是我到目前为止的定义,基本上只是构造函数:

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

当我编译时,我在刚才显示的构造函数中的某个未知位置得到这些错误(因为它是文件中唯一的代码,我在编译时遇到这些错误):

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>          ]

我无法理解什么是错的。从我的main测试时,我的FileMgr工作正常,那么为什么编译器在使用另一个类的FileMgr时会像那样跳转?

4 个答案:

答案 0 :(得分:2)

我相信你的问题就在这一行:

manager = FileMgr(inFileName, outFileName);

FileMgr中的代码看,FileMgr似乎有一个流input作为数据成员。当您执行上面的行时,您将调用FileMgr的赋值运算符,默认情况下它将尝试一次复制一个数据成员。但是,无法访问流的复制功能(它们标记为private且未实现)。您获得的错误几乎肯定是由于C ++编译器注意到它需要复制流,但由于复制功能无法访问而无法执行此操作。

要更改此设置,请尝试在构造函数的成员初始值设定项列表中初始化manager

Dictionary::Dictionary(string inFileName, string outFileName) 
    : manager(inFileName, outFileName) {
    /* ... */
}

这将使用给定参数初始化manager,而不是尝试使用正确的参数分配 manager对象。

希望这有帮助!

答案 1 :(得分:1)

bool FileMgr::writeFileToList(list<string> &l);

FileMgr::writeFileToList通过引用收到list<string>类型的参数。

所以你应该这样做 -

list<string> dictionary;
if( manager.writeFileToList(&dictionary) )
                            ^ error. You should not use & symbol here.

参数类型不是list<string>*l来发送地址。

答案 2 :(得分:1)

只需写下:

if( manager.writeFileToList(dictionary) );

&不需要。事实上,这会导致错误!

顺便说一下,你的std::copy写得不正确。这是正确的:

copy(istream_iterator<string>(input), istream_iterator<string>(), std::back_inserter(l));

注意最后一个参数。它是std::back_inserter(l)

答案 3 :(得分:0)

if( manager.writeFileToList(&dictionary) )应更改为

if( manager.writeFileToList(dictionary) )

请注意,您无法将Type*转换为Type&