Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/163.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 如何使用匿名C++;匿名结构的并集_C++_Struct_Unions - Fatal编程技术网

C++ 如何使用匿名C++;匿名结构的并集

C++ 如何使用匿名C++;匿名结构的并集,c++,struct,unions,C++,Struct,Unions,我需要有关访问结构中的联合的语法的帮助,如下所示。编译器抱怨我需要在下面命名“innerStruct”定义,而不是使用匿名的内部结构。请有人解释一下规则,以及我如何初始化构造函数字段和命名位字段元素。我有一个来显示代码 不幸的是,代码未编译,因为它指示以下错误: g++ -std=c++14 -O2 -Wall -pedantic -pthread main.cpp && ./a.out main.cpp: In constructor 'Foo::Foo(uint8_t, ui

我需要有关访问结构中的联合的语法的帮助,如下所示。编译器抱怨我需要在下面命名“innerStruct”定义,而不是使用匿名的内部结构。请有人解释一下规则,以及我如何初始化构造函数字段和命名位字段元素。我有一个来显示代码

不幸的是,代码未编译,因为它指示以下错误:

g++ -std=c++14 -O2 -Wall -pedantic -pthread main.cpp && ./a.out
main.cpp: In constructor 'Foo::Foo(uint8_t, uint8_t)':
main.cpp:38:11: error: class 'Foo' does not have any field named 'asUint8'
         , asUint8(aBitFields)
           ^
main.cpp: In function 'std::ostream& operator<<(std::ostream&, const Foo&)':
main.cpp:54:83: error: 'const struct Foo' has no member named 'innerStruct'
             << "], mA[0x" << std::setw(2) << std::setfill('0') << std::hex << rhs.innerStruct.mA
                                                                                   ^
main.cpp:55:83: error: 'const struct Foo' has no member named 'innerStruct'
             << "], mB[0x" << std::setw(2) << std::setfill('0') << std::hex << rhs.innerStruct.mB
                                                                                   ^
main.cpp:56:83: error: 'const struct Foo' has no member named 'innerStruct'
             << "], mC[0x" << std::setw(2) << std::setfill('0') << std::hex << rhs.innerStruct.mC
                                                                                   ^
main.cpp:57:83: error: 'const struct Foo' has no member named 'innerStruct'
             << "], mD[0x" << std::setw(2) << std::setfill('0') << std::hex << rhs.innerStruct.mD
                                                                                   ^

struct Foo {
    // fields
    uint8_t mType;
    union innerUnion_t {
        struct innerStruct_t {
            uint8_t mA : 2;
            uint8_t mB : 1;
            uint8_t mC : 2;
            uint8_t mD : 3;
        } innerStruct;
        uint8_t asUint8;
    } innerUnion;

    // constructor
    explicit Foo() = default;

    // constructor
    explicit Foo(
        const uint8_t aType,
        const uint8_t aBitFields)
        : mType(aType)
        , asUint8(aBitFields)
    {}

    /**
     * Stream insert operator<p>
     *
     * @param os     [in,out] output stream
     * @param rhs    [in] Foo to send to the output
     *               stream.
     *
     * @return a reference to the updated stream
     */
    friend std::ostream& operator<<(
        std::ostream& os, const Foo& rhs) {
        os  << "Foo"
            << ": type[0x" << std::setw(2) << std::setfill('0') << std::hex << rhs.mType
            << "], mA[0x" << std::setw(2) << std::setfill('0') << std::hex << rhs.innerStruct.mA
            << "], mB[0x" << std::setw(2) << std::setfill('0') << std::hex << rhs.innerStruct.mB
            << "], mC[0x" << std::setw(2) << std::setfill('0') << std::hex << rhs.innerStruct.mC
            << "], mD[0x" << std::setw(2) << std::setfill('0') << std::hex << rhs.innerStruct.mD
            << "]";
        return os;
    }
};
main.cpp: In constructor 'Foo::Foo(uint8_t, uint8_t)':
main.cpp:39:21: error: expected '(' before '.' token
         , innerUnion.innerStruct.mA(1)
                     ^
main.cpp:39:21: error: expected '{' before '.' token

您只需删除代码中的一些单词:

#include <memory>
#include <iomanip>
#include <iostream>
#include <string>
#include <vector>

template<typename T>
std::ostream& operator<<(std::ostream& os, const std::vector<T>& vec)
{
    for (auto& el : vec)
    {
        os << el << ' ';
    }
    return os;
}

struct Foo {
    // fields
    uint8_t mType;
    union {
        struct {
            uint8_t mA : 2;
            uint8_t mB : 1;
            uint8_t mC : 2;
            uint8_t mD : 3;
        } innerStruct;
        uint8_t asUint8;
    };

    // constructor
    explicit Foo() = default;

    // constructor
    explicit Foo(
        const uint8_t aType,
        const uint8_t aBitFields)
        : mType(aType)
        , asUint8(aBitFields)
    {}

    /**
     * Stream insert operator<p>
     *
     * @param os     [in,out] output stream
     * @param rhs    [in] Foo to send to the output
     *               stream.
     *
     * @return a reference to the updated stream
     */
    friend std::ostream& operator<<(
        std::ostream& os, const Foo& rhs) {
        os  << "Foo"
            << ": type[0x" << std::setw(2) << std::setfill('0') << std::hex << rhs.mType
            << "], mA[0x" << std::setw(2) << std::setfill('0') << std::hex << rhs.innerStruct.mA
            << "], mB[0x" << std::setw(2) << std::setfill('0') << std::hex << rhs.innerStruct.mB
            << "], mC[0x" << std::setw(2) << std::setfill('0') << std::hex << rhs.innerStruct.mC
            << "], mD[0x" << std::setw(2) << std::setfill('0') << std::hex << rhs.innerStruct.mD
            << "]";
        return os;
    }
};

int main()
{
    std::vector<std::string> words = {
        "Hello", "from", "GCC", __VERSION__, "!"
    };
    std::cout << words << std::endl;

    auto pFoo = std::make_unique<Foo>();
    std::cout << *pFoo << std::endl;
}
#包括
#包括
#包括
#包括
#包括
模板

std::ostream&operator注意:字段名表明您打算使用联合别名。这是标准C++中的未定义行为;只有最近写的工会会员才能阅读。此外,位字段的顺序和间距是由实现定义的。谢谢,但我可能没有问这个问题,但在构造函数中,我如何显式初始化命名位字段?正如您所看到的,我初始化了asUnit9字段,它与所有组合位字段同义。@johnco3:
Foo::Foo():innerStruct.mA(3){
我在头文件中尝试了以下方法,但它没有编译-我确实按照以下方法初始化了它,尽管我相信您在最后一段代码中是正确的,我在注释中的建议是错误的。