Please fix the following code. When I try to compile it, it show following error
ID: 3672428 • Letter: P
Question
Please fix the following code.
When I try to compile it, it show following errors!
ERRORS
-----------------------------------------------------------
../src/Time.cpp: In constructor ‘Time::Time()’:
../src/Time.cpp:97:6: error: class ‘Time’ does not have any field named ‘t_ms’
: t_ms{0}, is_24_hr{false}
^
../src/Time.cpp:97:6: warning: extended initializer lists only available with -std=c++11 or -std=gnu++11 [enabled by default]
../src/Time.cpp:97:15: error: class ‘Time’ does not have any field named ‘is_24_hr’
: t_ms{0}, is_24_hr{false}
^
../src/Time.cpp:97:15: warning: extended initializer lists only available with -std=c++11 or -std=gnu++11 [enabled by default]
../src/Time.cpp: In constructor ‘Time::Time(long int)’:
../src/Time.cpp:105:6: error: class ‘Time’ does not have any field named ‘t_ms’
: t_ms{time}, is_24_hr{false}
^
../src/Time.cpp:105:6: warning: extended initializer lists only available with -std=c++11 or -std=gnu++11 [enabled by default]
../src/Time.cpp:105:18: error: class ‘Time’ does not have any field named ‘is_24_hr’
: t_ms{time}, is_24_hr{false}
^
../src/Time.cpp:105:18: warning: extended initializer lists only available with -std=c++11 or -std=gnu++11 [enabled by default]
../src/Time.cpp: In constructor ‘Time::Time(int, int, int, int)’:
../src/Time.cpp:149:4: error: ‘t_ms’ was not declared in this scope
t_ms = h_conv + _conv + s_conv + milli;
^
../src/Time.cpp:151:4: error: ‘is_24_hr’ was not declared in this scope
is_24_hr = false;
^
../src/Time.cpp: In member function ‘long int Time::asLong() const’:
../src/Time.cpp:167:11: error: ‘t_ms’ was not declared in this scope
return t_ms;
^
../src/Time.cpp: In member function ‘std::string Time::toString() const’:
../src/Time.cpp:181:8: error: ‘is_24_hr’ was not declared in this scope
if (is_24_hr)
^
../src/Time.cpp:185:28: error: ‘t_ms’ was not declared in this scope
hrs = time_in_24hrs(t_ms);
^
../src/Time.cpp:205:26: error: ‘t_ms’ was not declared in this scope
if (time_in_12hrs(t_ms) == 0)
^
../src/Time.cpp:213:26: error: ‘t_ms’ was not declared in this scope
min = time_in_min(t_ms);
^
../src/Time.cpp: In member function ‘void Time::set24Hour(bool)’:
../src/Time.cpp:245:4: error: ‘is_24_hr’ was not declared in this scope
is_24_hr = value;
^
../src/Time.cpp: In member function ‘bool Time::is24Hour() const’:
../src/Time.cpp:253:11: error: ‘is_24_hr’ was not declared in this scope
return is_24_hr;
^
../src/Time.cpp: In member function ‘long int Time::asLong() const’:
../src/Time.cpp:169:1: warning: control reaches end of non-void function [-Wreturn-type]
}
^
../src/Time.cpp: In member function ‘bool Time::is24Hour() const’:
../src/Time.cpp:255:1: warning: control reaches end of non-void function [-Wreturn-type]
}
^
make: *** [src/Time.o] Error 1
---------------------------------------------------------------
/* Time.cpp */
#include <iostream>
#include <typeinfo>
#include <string>
#include "Time.h"
#include <sstream>
using namespace std;
//////////////////////Conversion methods///////////////////////////////////////
long hrs_to_ms(int i)
{
if (i < 0)
cerr << "Method hrs_to_ms: non-positive value given. ";
return i*60*60*1000; //hrs-->min-->sec-->ms
}
long min_to_ms(int i)
{
if (i < 0)
cerr << "Method min_to_ms: non-positive value given. ";
return i*60*1000;
}
long sec_to_ms(int i)
{
if (i < 0)
cerr << "Method sec_to_ms: non-positive value given. ";
return i*1000;
}
int time_in_ms(long t)
{
return t % 1000;
}
int time_in_sec(long t)
{
return (t/1000)%60;
}
int time_in_min(long t)
{
return (t/1000/60)%60;
}
int time_in_24hrs(long t)
{
return (t/1000/60/60)%24;
}
int time_in_12hrs(long t)
{
return (t/1000/60/60)%12;
}
////////////////////////End Conversion Methods//////////////
//Default Constructor: Time is at 0 ms, not 24 hr format
Time::Time()
: t_ms(0), is_24_hr(false)
{}
//Overloaded Constructor: milliseconds only
Time::Time(long time)
: t_ms(time), is_24_hr(false)
{
if (time < 0)
cerr << "Time Constructor: non-positive value given for time.";
}
//Overloaded Constructor: Hours, minutes, seconds, ms format
Time::Time(int hours, int minutes, int seconds, int milli )
{
//Error checking: positive values
if (hours < 0 || minutes < 0 || seconds < 0 || milli < 0)
cerr <<"Time Constructor: non-positive value given ";
if (hours > 23)
cerr << "Time Constructor: invalid hour value ";
if (minutes >= 60)
cerr << "Time Constructor: invalid minute value ";
if (seconds > 60)
cerr << "Time Constructor: invalid seconds value ";
if (milli > 999)
cerr << "Time Constructor: invalid milliseconds value ";
long h_conv = hrs_to_ms(hours);
long m_conv = min_to_ms(minutes);
long s_conv = sec_to_ms(seconds);
t_ms = h_conv + m_conv + s_conv + milli;
is_24_hr = false;
}
//Destructor
Time::~Time(void)
{}
////////////////////////////////Methods////////////////////////////////////////////
long Time::asLong() const
{
return t_ms;
}
string Time::toString() const
{
int hrs, min, sec, ms;
ostringstream s;
/*Hour assignment: check for 24 hour flag, adjust hour assignment an AM/PM output */
if (is_24_hr)
{
hrs = time_in_24hrs(t_ms);
min = time_in_min(t_ms);
sec = time_in_sec(t_ms);
ms = time_in_ms(t_ms);
s << hrs << ":" << min << ":" << sec << ":" << ms;
}
/* Output as 12-hour time, add AM/PM to output string */
else
{
/* Check for midnight state */
if (time_in_12hrs(t_ms) == 0)
hrs = 12;
else
hrs = time_in_12hrs(t_ms);
min = time_in_min(t_ms);
sec = time_in_sec(t_ms);
ms = time_in_ms(t_ms);
/* AM/PM check */
/* Check the time using time_in_24hrs, not time_in_12hrs, which is always < 12 */
if (time_in_24hrs(t_ms) < 12)
s << hrs << ":" << min << ":" << sec << ":" << ms << " AM";
else
s << hrs << ":" << min << ":" << sec << ":" << ms << " PM";
}
return s.str();
}
void Time::set24Hour(bool value)
{
/* DEBUG */
//cout << "set24Hour: value is " << value;
is_24_hr = value;
}
bool Time::is24Hour() const
{
return is_24_hr;
}
Explanation / Answer
where is Time.h ?