CUTE error when printing an enumeration

Added by jim elliott over 3 years ago

With this enumeration:
enum class RCC_BDCR_RTCSEL {
MASK = 0b1100000000, // Position of field within word.
NONE = 0b00, ///< No clock
LSE = 0b01, ///< LSE oscillator clock used as RTC clock
LSI = 0b10, ///< LSI oscillator clock used as RTC clock
HSE = 0b11, ///< HSE oscillator clock divided by 128 used as RTC clock
MASK_CPL = ~MASK // Complement of the mask.
};

and this test code
RCC_BDCR_RTCSEL source = rcc.rtc_clock_source();
cout << source << endl;
ASSERT_EQUAL(RCC_BDCR_RTCSEL::NONE, source); // HSI selected but not reported.

the output is:

#starting get_RTC_source_test
3

#failure get_RTC_source_test C:\Cortex\RCC_Unit_Test\RCC_Unit_Test.cpp:458 get_R
TC_source_test: RCC_BDCR_RTCSEL::NONE == source expected: no operator<<(os
tream&, STM32F1::RCC_BDCR_RTCSEL) but was: no operator<<(ostream&,
STM32F1::RCC_BDCR_RTCSEL)

The expected and actual values are not equal so I expect a test error, but I'm wanting the error message to show numbers.

Here is operator<< which works with cout:
std::ostream& operator << (std::ostream& os, STM32F1::RCC_BDCR_RTCSEL obj) {
os << static_cast<int>(obj);
return os;
}


Replies (2)

RE: CUTE error when printing an enumeration - Added by Thomas Corbat over 3 years ago

Hi

That's a C++ problem. The << operator cannot be resolved as it is not defined in the scope of RCC_BDCR_RTCSEL. The following should solve your problem:

namespace STM32F1 {
  std::ostream& operator << (std::ostream& os, STM32F1::RCC_BDCR_RTCSEL obj) {
    os << static_cast<int>(obj);
    return os;
  }
}

(1-2/2)