c++ - 错误 C2246 : cannot access private member

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

我的猜测是它与此有关。

最佳答案

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

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

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

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

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

编辑 - 您可能应该将 FilePlainText 的复制构造函数设置为 private 以防止尝试复制它。

关于c++ - 错误 C2246 : cannot access private member,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14261273/

相关文章:

c++ - 如何使用多个类简化游戏循环

c++ - QByteArray : Is it safe to read from the array ('[ ]' ) between writes on the DataStream ('<<' )? 上的 QDataStream

visual-c++ - 在 Visual Studio 2013 中显示构建时间?

c# - 无法运行 WCF https Web 服务

c++ - 添加到附加包含目录中的路径相对于什么?

c++ - 无法在 C++ 中编译 min_element

c++ - 像素数据传输 : 24BPP images and GL_UNPACK_ALIGNMENT set to 4

c++ - 为什么 std::unique_ptr 需要专门用于动态数组?

c++ - time_t 和 tm 转换中的值错误

visual-studio - 在 MSTest.exe 中通过命令行使用 .runsettings