C++ Tips
Enable Javascript to display Table of Contents.
Printing uint8_t
with iostream
When printing a uint8_t
with iostream it is normally treated as a char
and thus interpreted as ASCII. To avoid this, either add a unary plus (+
) in front of the variable or simply cast it to unsigned int
.
class ptp_clock_identity
{
public:
uint8_t id[8];
};
static inline std::ostream& operator<<(std::ostream& os, const class ptp_clock_identity& pi)
{
// need the static cast here, otherwise the uint8_t is printed as ASCII
os << std::hex << std::setw(2)
<< static_cast<unsigned int>(pi.id[0])
<< static_cast<unsigned int>(pi.id[1])
<< static_cast<unsigned int>(pi.id[2])
<< "."
<< static_cast<unsigned int>(pi.id[3])
<< static_cast<unsigned int>(pi.id[4])
<< "."
<< static_cast<unsigned int>(pi.id[5])
<< static_cast<unsigned int>(pi.id[6])
<< static_cast<unsigned int>(pi.id[7]);
return os;
}
Solving Compiler Errors
std::unique_ptr
causes error: invalid application of ‘sizeof’ to incomplete type ‘XXX’
This happens only if you haven't implemented a destructor in your cpp file. Thus a default destructor or a inline destructor in your header will cause this error.
Source: ortogonal.github.io