I have read many posts about LNK2019, but can't resolve this error.
Here is my code:
Time.h:
#ifndef PROJECT2_TIME_H
#define PROJECT2_TIME_H
#include<iostream>
using std::ostream;
namespace Project2
{
class Time
{
friend Time& operator+=(const Time& lhs, const Time& rhs);
friend ostream& operator<<(ostream& os, const Time& rhs);
public:
static const unsigned secondsInOneHour = 3600;
static const unsigned secondsInOneMinute = 60;
Time(unsigned hours, unsigned minutes, unsigned seconds);
unsigned getTotalTimeAsSeconds() const;
private:
unsigned seconds;
};
Time& operator+=(const Time& lhs, const Time& rhs);
ostream& operator<<(ostream& os, const Time& rhs);
}
#endif
Time.cpp:
#include "Time.h"
Project2::Time::Time(unsigned hours, unsigned minutes, unsigned seconds)
{
this->seconds = hours*secondsInOneHour + minutes*secondsInOneMinute + seconds;
}
unsigned
Project2::Time::getTotalTimeAsSeconds() const
{
return this->seconds;
}
Project2::Time&
Project2::operator+=(const Time& lhs, const Time& rhs)
{
Time& tempTime(unsigned hours, unsigned minutes, unsigned seconds);
unsigned lhsHours = lhs.seconds / Time::secondsInOneHour;
unsigned lhsMinutes = (lhs.seconds / 60) % 60;
unsigned lhsSeconds = (lhs.seconds / 60 / 60) % 60;
unsigned rhsHours = rhs.seconds / Time::secondsInOneHour;
unsigned rhsMinutes = (rhs.seconds / 60) % 60;
unsigned rhsSeconds = (rhs.seconds / 60 / 60) % 60;
return tempTime(lhsHours + rhsHours, lhsMinutes + rhsMinutes, lhsSeconds + rhsSeconds);
}
ostream&
Project2::operator<<(ostream& os, const Time& rhs)
{
unsigned rhsHours = rhs.seconds / Time::secondsInOneHour;
unsigned rhsMinutes = (rhs.seconds / 60) % 60;
unsigned rhsSeconds = (rhs.seconds / 60 / 60) % 60;
os << rhsHours << "h:" << rhsMinutes << "m:" << rhsSeconds << "s";
return os;
}
The main.cpp simply creates Time objects and uses the overloaded operators, doesn't seem like there can be problems(these codes are provided so are themselves good).
I have tried to remove the "&" behind all the "Time" symbol, and I got the same error.
And here is the error message:
Error 1 error LNK2019: unresolved external symbol "class Project2::Time & __cdecl tempTime(unsigned int,unsigned int,unsigned int)" (?tempTime@@YAAAVTime@Project2@@III@Z) referenced in function "class Project2::Time & __cdecl Project2::operator+=(class Project2::Time const &,class Project2::Time const &)" (??YProject2@@YAAAVTime@0@ABV10@0@Z) c:\Users\Eon-Gwei\documents\visual studio 2013\Projects\c++III_Project2_GW\c++III_Project2_GW\Time.obj c++III_Project2_GW
Time& tempTime(unsigned hours, unsigned minutes, unsigned seconds);
declares a function with the name tempTime
, and return tempTime(lhsHours + rhsHours, lhsMinutes + rhsMinutes, lhsSeconds + rhsSeconds);
calls that function. Since the function does not have an implementation anywhere, you get a linker error.
Since
Edit: Any sane implementation of operator +=
is presumably supposed to return a reference to the object it is invoked on, you should modify the object's member variables through this
rather than creating a new Time
, and return *this
.operator +=
would modify the left-hand-side operand, rather than creating a new object. I suggest you reconsider how your operator should work.