Please fix the following code. When I try to compile it, it show following error
ID: 3672443 • Letter: P
Question
Please fix the following code.
When I try to compile it, it show following errors for Time.cpp part!
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
---------------------------------------------------------------
/* This is Tim.h code */
#ifndef TIME_H_
#define TIME_H_
#include <iostream>
#include <string>
/**
* Time class
*
* The Time class contains time as hours:minutes:seconds:milliseconds (AM/PM).
*/
class Time
{
public:
/**
* Constructor with zero values
*/
Time();
/**
* Constructors with arguments
*/
Time(long time);
Time(int hours, int minutes, int seconds, int milli);
/**
* Deconstructor
*/
virtual ~Time();
/**
* Return time as a long value representing time in milliseconds
*/
long asLong() const;
/**
* Display as a string in the format hours:minutes:seconds:milliseconds.
* For example 1:45:30:56 PM
*
* The time is displayed as 24 hours if the 24 hour flag is set true.
*/
std::string toString() const;
/**
* Enable/disable 24 hour time display
*/
void set24Hour(bool value);
/**
* Return true if 24 hour time display is enabled
*/
bool is24Hour() const;
private:
bool is_24_hr;
long t_ms;
};
#endif /* TIME_H_ */
/* This is the 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;
}
/* This is main.cpp code */
#include <iostream>
#include <string>
#include "Time.h"
#include <sstream>
using namespace std;
int main()
{
/* Default constructor test */
cout << "Object test: ";
Time test;
if (test.is24Hour())
cout << "test is 24 hour formatted";
else
cout << "test is not 24 hour formatted";
cout << endl <<test.toString() << " ";
/* Overloaded test: asLong() test */
cout << "Object raw: ";
Time raw(14,02,50,600);
raw.set24Hour(true);
if (raw.is24Hour())
cout << "raw is 24 hour formatted ";
else
cout << "raw is not 24 hour formatted ";
cout << raw.toString() << " Time in ms: " << raw.asLong() << endl;
/* Overloaded test: 24 hour */
cout << "Object now: ";
Time now(13, 20, 04, 600);
now.set24Hour(true);
if (now.is24Hour())
cout << "now is 24 hour formatted";
else
cout << "now is not 24 hour formatted";
cout << endl <<now.toString() << " ";
/* Overloaded test: 12 hour */
cout << "Object t_money: ";
Time t_money (15, 55, 43, 900);
if (t_money.is24Hour())
cout << "t_money is 24 hour formatted";
else
cout << "t_money is not 24 hour formatted";
cout << endl <<t_money.toString() << " ";
/* Overloaded test: max time, min time [ALL WORKING, MIDNIGHT CASE HANDLED]*/
cout << "Object min: ";
Time min (0,0,0,0);
cout << min.toString() << " ";
min.set24Hour(true);
cout << min.toString() << " ";
cout << "Object max: ";
Time max (23, 59, 59, 999);
cout << max.toString() << " ";
max.set24Hour(true);
cout << max.toString() << " ";
/* Overloaded test: bad input */
cout << "Object bad1: ";
Time bad1 (-30, 80, 109, 1010); //Negative, out of bound values
cout << bad1.toString() << " ";
cout << "Object bad2: ";
Time bad2 ('T', 'a', 6, 509); //non-integer values
cout << bad2.toString() << " ";
cout << "Object bad3: ";
Time bad3 (14, 43, 50, 1010); //Out of bound only
cout << bad3.toString() << " ";
cout << "Object bad4: ";
Time bad4 (15, 45, 20, 50);
cout << bad4.toString() << " ";
}
Explanation / Answer
Hi, This is comiling fine except some warnings. This code is running fine. Just keep Time.h and Time.cpp in one folder and compile using g++ compiler. If still you are getting same issues then update your g++ compiler.
After running this program, I got below output:
Time Constructor: invalid minute value
Time Constructor: invalid seconds value
Time Constructor: invalid milliseconds value
Method hrs_to_ms: non-positive value given.
-4:-38:-9:-990 AM
Object bad2:
Time Constructor: invalid hour value
Time Constructor: invalid minute value
1:37:6:509 PM
Object bad3:
Time Constructor: invalid milliseconds value
2:43:51:10 PM
Object bad4:
3:45:20:50 PM