C++ LNK2019 未解析的外部符号
C++ LNK2019 unresolved external symbol
我看了很多关于LNK2019的帖子,但是无法解决这个错误。
这是我的代码:
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;
}
main.cpp只是简单的创建了Time对象,并使用了重载的运算符,好像没有什么问题(提供了这些代码,本身就不错)。
我试图删除所有 "Time" 符号后面的“&”,但我得到了同样的错误。
这是错误消息:
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);
声明一个名为 tempTime
的函数,return tempTime(lhsHours + rhsHours, lhsMinutes + rhsMinutes, lhsSeconds + rhsSeconds);
调用该函数。由于该函数在任何地方都没有实现,因此您会收到链接器错误。
因为 operator +=
大概是 return 对调用它的对象的引用,你应该通过 this
修改对象的成员变量而不是创建新的 Time
和 return *this
.
编辑: operator +=
的任何合理实现都会修改左侧操作数,而不是创建新对象。我建议你重新考虑你的运营商应该如何工作。
我看了很多关于LNK2019的帖子,但是无法解决这个错误。
这是我的代码:
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;
}
main.cpp只是简单的创建了Time对象,并使用了重载的运算符,好像没有什么问题(提供了这些代码,本身就不错)。
我试图删除所有 "Time" 符号后面的“&”,但我得到了同样的错误。
这是错误消息:
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);
声明一个名为 tempTime
的函数,return tempTime(lhsHours + rhsHours, lhsMinutes + rhsMinutes, lhsSeconds + rhsSeconds);
调用该函数。由于该函数在任何地方都没有实现,因此您会收到链接器错误。
因为
编辑: operator +=
大概是 return 对调用它的对象的引用,你应该通过 this
修改对象的成员变量而不是创建新的 Time
和 return *this
.operator +=
的任何合理实现都会修改左侧操作数,而不是创建新对象。我建议你重新考虑你的运营商应该如何工作。