3

i have to write a program in which i have to take time(hh:mm:ss) as string input and tokenize it using : as a delimiter and pass hours, minutes and seconds to a struct time and then display the total number of seconds. I have written the code but it's giving me error that i can't understand. This is the code:

#include<iostream>
#include<string>
using namespace std;

struct time{
   int hours;
   int minutes;
   int seconds;
  
   void Print_in_Seconds(){
       cout<<"Total Second: "<<hours*3600+minutes*60+seconds;
   }
};

time Tokenized_time(string str)
{
   time time_;
   // Get hours
   int hour1 = (int)str[1] - '0';
   int hour2 = (int)str[0] - '0';
   int hour = (hour2 * 10 + hour1 % 10);
   int count=0;
   int minute=0,second=0;
   for (int i=3; i <= 7; i++){
       if(i==3 || i==4){
       minute=minute*10+((int)(str[i]-'0'));  
       }
       else if(i==6 || i==7){
       second=second*10+((int)(str[i]-'0'));  
       }
   }
   time_.hours=hour;
   time_.minutes=minute;
   time_.seconds=second;
   return time_;

}

// Driver code
int main()
{
   time time1;
   cout<<"Enter Time- 00:00:00 Formate: ";
   string str;
   cin>>str;
   time time1=Tokenized_time(str);
   time1.print();
return 0;
}

and this is the error i'm getting

enter image description here

Any help?

  • 1
    A beautiful example of https://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice. `std::time` already exists as a function and you pulled in the whole `std` namespace. – AVH Nov 01 '20 at 17:09
  • @Darhuuk should i remove it? – Mahreen Athar Nov 01 '20 at 17:10
  • No, your issue lies elsewhere. – cigien Nov 01 '20 at 17:11
  • 1
    @Darhuuk That's what I thought too, but it [complains](https://godbolt.org/z/qdMhz7) anyway. – cigien Nov 01 '20 at 17:11
  • @cigien How do i fix this? i can't seem to pin point where exactly the error is – Mahreen Athar Nov 01 '20 at 17:12
  • 2
    It's a bug in gcc and clang. They bring in global `::time` function whether you use `using namespace std` or not (which they most definitely shouldn't). – n. m. could be an AI Nov 01 '20 at 17:15
  • @n.'pronouns'm. Oh, that's cool. I didn't know that, thanks :) – cigien Nov 01 '20 at 17:18
  • Maybe actually not a bug, the standard says any standard header may include any other standard header and this includes e.g. `ctime` which is then allowed to bring in global `::time` function. See https://gcc.gnu.org/bugzilla/show_bug.cgi?id=6257 – n. m. could be an AI Nov 01 '20 at 17:37

1 Answers1

5

time happens to be the name of a function in the standard C library (and incorporated into the C++ library), and by naming your struct the same, you're creating ambiguity, and according to the grammar rules of C++, the compiler must consider time to the function name, here, resulting in a compilation error.

The simplest solution is to pick a different name for your struct. You could also direct your compiler to explicitly parse time as a type:

struct time Tokenized_time(string str)

And as long as you're doing it, you might as well get rid of using namespace std in order to eliminate more opportunities for a similar kind of a headache, confusion, and unwelcome surprises.

EDIT: it may very well be the case that this is a bug in libstc++, but there's nothing you can do about it except to sit and wait for gcc to fix this bug. But you'll probably be able to work around it, like this, much faster...

Sam Varshavchik
  • 114,536
  • 5
  • 94
  • 148
  • You might want to mention that this is a compiler bug. `time` is not *supposed* to be visible without including it. – cigien Nov 01 '20 at 17:19
  • In addition to what is mentioned by Sam, you have a redefinition of time1 in after calling `Tokenized_time()` and `print` also is not part of the struct `Print_in_Seconds()` is so `time1.print()` won't work. – magmine Nov 01 '20 at 17:21
  • My code worked but it's giving the wrong output. It's supposed to convert the time into seconds but it's giving the wrong answer. Can you help with that? – Mahreen Athar Nov 01 '20 at 17:28
  • @magmine yes i corrected that print thing. The code is working now but it's giving the wrong output – Mahreen Athar Nov 01 '20 at 17:29
  • One question per stackoverflow.com question, please. You're welcome to post another question, but keep in mind that you must meet all requirements for a [mre], and all information in the question should be in plain text, no images or links to external web sites (you kind of violated this rule here, a little bit). – Sam Varshavchik Nov 01 '20 at 17:50