I defined a class as below with two member variables and two conversion operators (operator float and operator chrono::microseconds). But the code below only works if I comment out the float operator. Otherwise, it throws an error (cannot convert type). I can't figure out why?
#include <iostream>
#include <chrono>
using namespace std::chrono_literals;
class Peak {
public:
Peak (std::chrono::microseconds t, float magnitude)
: t_(t),
magnitude_(magnitude)
{
};
std::chrono::microseconds get_t() { return t_; }
//have to comment this out or I get an error
operator float() { return magnitude_; }
operator std::chrono::microseconds() {
return t_;
}
private:
std::chrono::microseconds t_{2us};
float magnitude_;
};
int main()
{
Peak a{3us, 100};
std::cout
<< "t is "
<< static_cast<std::chrono::microseconds>(a).count();
}