Conversion to int with static_cast in c++

Asked

Viewed 89 times

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 this std::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.

  • 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

  • @What’s the matter with the std::string("Jan")? He is building a string directly in the std::pair<std::string, Month>. Thus, the .second refers exactly to the second term of this pair, which in this case is the Month. 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 the tuple which is a generalization of pair

  • @Amadeus didn’t know this. This code is too bad, I would throw it away and start a new :)

  • @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

  • 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.

  • @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, of enum class and of std::pair?

Show 2 more comments

1 answer

1


Well, by directly answering your question, the correct way to use the static_cast, in this case, is to replace:

return static_cast<std::vector<int>itr.second>;

for:

return static_cast<int>(itr.second);

because you are converting an item from enum class Month for a whole.

Now, honestly, I don’t think there’s any point in doing that. The enum class was created exactly to avoid these unwanted conversions. The correct thing is that in the function GetMonth she returns a enum class Month. Or in other words, that she has the following signature:

Month GetMonth(const std::string&) const;

Now, if you really want an entire conversion, use the enum standard (without the class)

  • then I took this code but also I did not understand right because the guy who developed it wanted to convert to int, to min what is more logical and correct I just wanted to know how to fix the error within this type of conversation..

  • I honestly did not understand why it converts the code to int, but added Return static_cast<int>(Itr.Second); as you said and generated this error below: /tmp/ccWCXO05.o: In Function CurrentDateTime::CurrentDateTime()':&#xA;date.cxx:(.text+0xb8): undefined reference to vtable for Currentdatetime' /tmp/ccWCXO05.o: In Function main':&#xA;date.cxx:(.text+0x8ea): undefined reference to Currentdatetime::~Currentdatetime()' date.Cxx:(.text+0x90e): Undefined Reference to `Currentdatetime::~Currentdatetime()' collect2: error: Ld returned 1 Exit status

  • Apparently the author had the code already made for an older version of c++ and wanted to update it to a more current version without testing the code. This particular error refers to the absence of the definition of the destructor. Just set it, for example by placing {}

  • now went well but according to the original author of the code the program has to generate the following result below on the current day of the week: Output Current Day:Tue Current Date:4 Current Month:11 Current Year:2014 Current Hour:10 Current Min:43 Current Second:35 ###########################################so he generated this: bash-4.4$ . /date Current Day....: Current Date...: 0 Current Month..: 6556000 Current Year...: 0 Current Hour...: 0 Current Min...: 0 Current Second.: 0

  • Well, now it’s with the original author of the program. His question was about how to use the static_cast correct in this case. Whether the program is working correctly or not is another problem

  • actually wanted to make this program work properly.

  • I guess that’s another question

  • question is not a statement .... I did not understand why it is not returning the hour months correctly get if now it is compiling beauty...

  • Compiling does not mean that it will work. It just means that from a language point of view, the constructions are correct. To know what his problem is, you have to do an analysis of what the program is doing. That’s why I said that’s another question. It is not in the scope of this. For example, I’m not in the time to debug the program. So, I can’t tell the pq it is not working

  • I figured it could all be solved on one topic...

Show 5 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.