错误C2246:无法访问私有成员

时间:2013-01-10 15:24:30

标签: c++ visual-c++ visual-studio-2010

我对这个编译器错误感到困惑,我无法弄清楚我的生活是怎么回事。整个错误对我来说并没有多大意义,而且大多数代码都是从以前工作正常的项目中复制的,这使得它更加奇怪。

错误和代码如下:

1>ClCompile:
1>  main.cpp
1>c:\program files (x86)\microsoft visual studio 10.0\vc\include\fstream(1347): error C2248: 'std::basic_ios<_Elem,_Traits>::basic_ios' : cannot access private member declared in class '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(176) : see declaration of 'std::basic_ios<_Elem,_Traits>::basic_ios'
1>          with
1>          [
1>              _Elem=char,
1>              _Traits=std::char_traits<char>
1>          ]
1>          This diagnostic occurred in the compiler generated function 'std::basic_fstream<_Elem,_Traits>::basic_fstream(const std::basic_fstream<_Elem,_Traits> &)'
1>          with
1>          [
1>              _Elem=char,
1>              _Traits=std::char_traits<char>
1>          ]
1>
1>Build FAILED.

这是 main.cpp *的非常小的代码:

#include <Mage/File/PlainText.h>
#include <cstdio>

int main(int argc, char** argv) {
    Mage::FilePlainText file = Mage::FilePlainText("shadow_pcf_gaussian_fragment.glsl");
    printf("%s\n", file.content().c_str());

    while (true) { }

    return 0;
}

代码 file.content():

Mage::String FilePlainText::content() {
    Mage::String src;

    try {
        open();

        // We allocate enough memory to copy the entire content to memory.
        mHandle.seekg(0, std::ios::end);
        src.reserve(mHandle.tellg());

        // Set pointer to 0 and copy to memory.
        mHandle.seekg(0, std::ios::beg);
        src.assign((std::istreambuf_iterator<char>(mHandle)), 
                    std::istreambuf_iterator<char>());

        close();
    } catch (Mage::Exception& e) {
        throw e;
    }

    return src;
}

我的猜测是它与此有关。

1 个答案:

答案 0 :(得分:3)

我猜FilePlainText是(直接或间接)从fstream派生的,或者是fstream类型的成员(在这种情况下,复制构造函数会尝试复制)是不可复制的,所以

Mage::FilePlainText file = Mage::FilePlainText("shadow_pcf_gaussian_fragment.glsl");

是非法的。为什么不简单:

Mage::FilePlainText file("shadow_pcf_gaussian_fragment.glsl");

我怀疑你真的想要复制初始化。

编辑 - 您应该制作FilePlainText private的复制构造函数,以防止尝试复制它。