1
Doing a search found something related to this error. I have the following error below:
ERROR: date.Cxx: In Member Function ːint Currentdatetime::Getmonth(const string&)': date.Cxx:125:18: error: cannot Convert ːconst Month' to ːint' in Return
related to:
int CurrentDateTime::GetMonth(const std::string& input)
{
for(const auto& itr : monthinfo)
{
if(itr.first == input)
return static_cast<std::vector<int>itr.second>;
}
}
where montinfo comes from:
enum class Month
{
Jan = 1, Feb, Mar, Apr, May, Jun, Jul, Aug, Sep, Oct, Nov, Dec
};
// Datastructure for string to num conversion in month(.i.e."Mar" Month to 3)
std::array<std::pair<std::string, Month>, 12> monthinfo =
{
std::make_pair(std::string("Jan"), Month::Jan),
std::make_pair(std::string("Feb"), Month::Feb),
std::make_pair(std::string("Mar"), Month::Mar),
std::make_pair(std::string("Apr"), Month::Apr),
std::make_pair(std::string("May"), Month::May),
std::make_pair(std::string("Jun"), Month::Jun),
std::make_pair(std::string("Jul"), Month::Jul),
std::make_pair(std::string("Aug"), Month::Aug),
std::make_pair(std::string("Sep"), Month::Sep),
std::make_pair(std::string("Oct"), Month::Oct),
std::make_pair(std::string("Nov"), Month::Nov),
std::make_pair(std::string("Dec"), Month::Dec)
};
I read that Enum class does not implicitly convert to int because it is purposeful, it was necessary to use a static_cast in the Itr.Second.
I tried to do something like this would really be it?
int CurrentDateTime::GetMonth(const std::string& input)
{
for(const auto& itr : monthinfo)
{
if(itr.first == input)
return static_cast<std::vector<int>itr.second>;
}
}
Some reason to do
std::string("Jan")
? What is thisstd::vector<int>
? Where did this one come from.second
? I think it has better ways of making this whole structure, it has better structures to do this way too.– Maniero
found this code so I decided to test it, put in Pastebin found the same when I was researching some forms of mecher with vectors dates and but it returns me this error and a boys told me I should static_cast to convert Enun class into int, but I have no idea how to do it really Pastebin.com/Mhf0ec71
– dark777
@What’s the matter with the
std::string("Jan")
? He is building a string directly in thestd::pair<std::string, Month>
. Thus, the.second
refers exactly to the second term of thispair
, which in this case is theMonth
. This structure is perfectly valid and widely used in c++. So much so that in c++17 is the standard for what they call Structured Binding, but in this case, they use thetuple
which is a generalization ofpair
– user5299
@Amadeus didn’t know this. This code is too bad, I would throw it away and start a new :)
– Maniero
@bigown I can’t tell if the code is good or not, but the structures used are very good and it’s the idiomatic way of doing
– user5299
I wouldn’t do it like that under any circumstances, this looks like an onion. I’ve never seen code like this in C++, let alone something similar in other languages, so I find it odd that you say this, which contradicts your answer.
– Maniero
@bigown I don’t want to make this an extensive discussion, but I was curious, which part contradicts? I also didn’t understand the part about the 'onion'. You’re talking about the
std::array
, ofenum class
and ofstd::pair
?– user5299