C++ - 错误:"BO3"未在此范围内声明

C++ - error: 'BO3' was not declared in this scope

本文关键字:范围内 声明 BO3 错误 C++      更新时间:2023-10-16
class BO2Offsets
{
    public:
        struct Prestige
        {
            u32 Offset { 0x000000 };
            char One { 0x01 },
                Two { 0x02 },
                Three { 0x03 },
                Four { 0x04 },
                Five { 0x05 },
                Six { 0x06 },
                Seven { 0x07 },
                Eight { 0x08 },
                Nine { 0x09 },
                Ten { 0x0A },
                MasterPrestige { 0x0B },
                CheatedPrestige { 0x0C };
        };
};
BO2Offsets BO2;

主.c

BO2 *BO3;

我正在创建一个新元素作为 BO2,但它返回了一个错误:

error: 'BO3' was not declared in this scope

我该如何解决这个问题?
编辑:当我像这样声明 BO3 时:

BO2Offsets *BO3;

我像这样使用 BO3:

BO3->Prestige->Offset

我收到一个错误:错误:无效使用"结构 BO2偏移量::P声望"|

你需要

一个typedef。您现在拥有的是一个变量声明。

BO2Offsets BO2;更改为typedef BO2Offsets BO2;


至于你的第二个问题,你不能使用这样的Prestige,因为它不是数据成员。如果需要访问内部类型,则需要BO2Offsets::Prestige::Offset

BO2 不是类型,只是变量名,编译器需要类型。

BO2Offsets * B03;

编辑:其次用

BO3->Prestige.anyName.Offset;

编辑2;好的伙计们,如果每个人都编辑他的内容,我也编辑:结构引入不当,字段无法访问。现在这样的概念在现代 GCC 上编译正常。顺便说一句,这种引入Prestige不是太有用,类似于"匿名类"(在某种程度上(。我倾向于在更高级别声明结构/类。在这个片段中可以省略蜜蜂。
但是为字段命名(我的anyName(对于访问他的内部字段很重要。

class BO2Offsets
    {
        public:
            struct Prestige
            {
                u32 Offset { 0x000000 };
                char One { 0x01 },
                    Two { 0x02 },
                    Three { 0x03 },
                    Four { 0x04 },
                    Five { 0x05 },
                    Six { 0x06 },
                    Seven { 0x07 },
                    Eight { 0x08 },
                    Nine { 0x09 },
                    Ten { 0x0A },
                    MasterPrestige { 0x0B },
                    CheatedPrestige { 0x0C };
            } anyName; // here !!!
    };

BO3 在你的情况下什么都不是。

首先需要铸造 BO3。

BO2Offsets BO3;

BO2Offsets *BO3;