编译错误友元类无法访问字段



我正在尝试编译QT5.3
有问题的文件是qv4executableallocator_p.h和qv4可执行文件分配器.cpp。标题中的相关代码片段如下

 struct Allocation{
    Allocation()
    : addr(0)
    , size(0)
    , free(true)
    , next(0)
    , prev(0)
    {}
    void *start() const;
    void invalidate() { addr = 0; }
    bool isValid() const { return addr != 0; }
    void deallocate(ExecutableAllocator *allocator);
    private:
        ~Allocation() {}
        friend class ExecutableAllocator;
        Allocation *split(size_t dividingSize);
        bool mergeNext(ExecutableAllocator *allocator);
        bool mergePrevious(ExecutableAllocator *allocator);
        quintptr addr;
        uint size : 31; // More than 2GB of function code? nah :)
        uint free : 1;
        Allocation *next;
        Allocation *prev;
};  

在 cpp 函数ExecutableAllocator::ChunkOfPages::~ChunkOfPages()中,我在尝试访问 alloc->next 时出现编译错误。

QV4::ExecutableAllocator::Allocation* QV4::ExecutableAllocator::Allocation::next’ is private

代码可以在 https://qt.gitorious.org/qt/qtdeclarative/source/be6c91acc3ee5ebb8336b9e79df195662ac11788:src/qml/jsruntime 在线查看

我的 gcc 版本相对较旧...4.1
这是问题还是我环境中的其他问题。我想要一条前进的道路。我被这个编译器困住了,因为它是我必须在目标平台上使用的编译器

我猜QV4::ExecutableAllocator::ChunkOfPages结构没有直接与Allocation友好,因此您无法在C++11标准之前的C++中访问Allocation的私有数据。

尝试将friend struct ExecutableAllocator::ChunkOfPages添加到Allocation定义中,这应该可以解决问题。

在 C++11 中处理嵌套类的方式略有变化(引自 cppreference.com):

在 C++11 之前,类 T 的友元的嵌套类内的成员声明和定义无法访问类 T 的私有和受保护成员,但某些编译器即使在 C++11 之前的模式下也接受它。

这可以解释为什么这在新编译器中有效,但在旧编译器中不起作用。

最新更新