Я прочитал много сообщений о LNK2019, но не могу решить эту ошибку.
Вот мой код:
Время.ч:
#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
Время.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;
}
main.cpp просто создает объекты Time и использует перегруженные операторы, не похоже, что могут быть проблемы (эти коды предоставлены, поэтому сами по себе хороши).
Я попытался удалить «&» за всем символом «Время», и я получил ту же ошибку.
А вот сообщение об ошибке:
Ошибка 1 ошибка LNK2019: неразрешенный внешний символ "class Project2::Time & __cdecl tempTime(unsigned int,unsigned int,unsigned int)" (?tempTime@@YAAAVTime@Project2@@III@Z), на который ссылается функция "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