Reputation: 2642
I need help with the syntax to access a union within a struct as shown below. The compiler complains that I need must name the 'innerStruct' definition below as opposed to having an anonymous inner struct. Could someone please explain the rules and how I can initialize the constructor fields and name the bit field elements. I have a live coliru demo to show the code.
Unfortunately the code does not compile as it indicates the following error:
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;
}
};
EDIT
Note: following the suggested answer with a slight change to the struct definition, I also have an additional question about what the syntax should be to initialize or refer to the bit fields mA -> mD, I tried in the constructor using the following syntax but it doesn't compile either. I tried to name the innerUnion and explictly refer to its named nested struct as innerUnion.innerStruct.mA(1) - see second live demo
It gives the following error:
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
Upvotes: 1
Views: 490
Reputation: 81926
You just need to remove some words in your code:
#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;
}
Upvotes: 2