This is my method of adding 2 Duration objects together in the format (HH,MM,SS).
inline ostream& operator<<(ostream& ostr, const Duration& d){
return ostr << d.getHours() << ':' << d.getMinutes() << ':' << d.getSeconds();
}
Duration operator+ (const Duration& x, const Duration& y){
if ( (x.getMinutes() + y.getMinutes() >= 60) && (x.getSeconds() + y.getSeconds() >= 60) ){
Duration z( (x.getHours() + y.getHours() + 1), (x.getMinutes() + y.getMinutes() + 1 - 60), (x.getSeconds() + y.getSeconds() - 60) );
return z;
}
else if (x.getSeconds() + y.getSeconds() >= 60){
Duration z( (x.getHours() + y.getHours()), (x.getMinutes() + y.getMinutes() + 1), (x.getSeconds() + y.getSeconds() - 60) );
return z;
}
else if (x.getMinutes() + y.getMinutes() >= 60){
Duration z( (x.getHours() + y.getHours() + 1), (x.getMinutes() + y.getMinutes() - 60), (x.getSeconds() + y.getSeconds()) );
return z;
}
else{
Duration z( (x.getHours() + y.getHours()), (x.getMinutes() + y.getMinutes()), (x.getSeconds() + y.getSeconds()) );
return z;
}
}
In my main method i have:
Duration dTest4 (01,25,15);
Duration result = dTest4+dTest4;
cout << result << endl;
Unfortunately when i run the program i get this error:
error LNK2019: unresolved external symbol "class std::basic_ostream<char,struct std::char_traits<char> > & __cdecl operator<<(class std::basic_ostream<char,struct std::char_traits<char> > &,class Duration const &)" (??6@YAAAV?$basic_ostream@DU?$char_traits@D@std@@@std@@AAV01@ABVDuration@@@Z) referenced in function _wmain 1>C:\Users\...exe : fatal error LNK1120: 1 unresolved externals
I want to be able to add two times together in individual entities. Ie. The hours together, then the minutes, then the seconds. Hence the if-else to deal with when 2 sets of minutes exceed the 60min cap of an hour…
Any help would be appreciated.
The problem is here:
cout << result << endl;You haven’t defined an overloaded version of
std::ostream& operator<<for writing yourDurationobject to an ostream. Something like this should do it: