C++ 操作员过载问题

C++ 操作员过载问题,c++,visual-studio-2012,C++,Visual Studio 2012,在person.h中,在名为person的类的公共部分下,我有以下内容: bool operator < (person& currentPerson); bool person::operator < (person& currentPerson) { return age < currentPerson.age; } bool操作员

在person.h中,在名为person的类的公共部分下,我有以下内容:

bool operator < (person& currentPerson);
bool person::operator < (person& currentPerson)
{
   return age < currentPerson.age;
}
bool操作员<(人员和当前人员);
在person.cpp中,我有:

bool operator < (person& currentPerson);
bool person::operator < (person& currentPerson)
{
   return age < currentPerson.age;
}
bool person::operator<(person和currentPerson)
{
返回年龄
当我编译它时,我会得到一个链接器错误,但前提是我实际使用了操作符。 有人能告诉我我做错了什么吗

这是错误消息

1>FunctionTemplates.obj : error LNK2019: unresolved external symbol "public: bool __thiscall person::operator<(class person const &)" (??Mperson@@QAE_NABV0@@Z) referenced in function "class person __cdecl max(class person &,class person &)" (?max@@YA?AVperson@@AAV1@0@Z)
1>c:\users\kenneth\documents\visual studio 2012\Projects\FunctionTemplates\Debug\FunctionTemplates.exe : fatal error LNK1120: 1 unresolved externals

1>FunctionTemplates.obj:error LNK2019:unresolved external symbol“public:bool\uu thiscall person::operator在您的代码中的某个地方,当使用function
max
时,您将一个人与一个临时人员进行比较。为此,您需要使用const引用

bool operator < (const person& currentPerson)  const;
                 ^^^^                          ^^^^^^ //This wont hurt too
bool运算符<(const person和currentPerson)const;
^^^^^^^^^^//这不会太疼

bool person::operator<(const person和currentPerson)
//                       ^^^^^
{
返回年龄
您如何使用运算符,以及正在使用的
person
(右操作数)对象的类型是什么?请尝试使用
const
将签名更新为
bool运算符<(const person¤tPerson)什么使person是临时的?person对象都不是常量。您没有将它们声明为常量。但有时STL算法会创建临时对象。例如
p
除非运算符函数接受常量引用,否则将无法工作。顺便说一句,这将修复它,我只想了解更多关于我最初拥有的是什么的信息wrong@kennycoc之所以
p
不起作用,是因为您无法参考临时
人员(1)
它实际上没有地址,并且是动态创建的。编译器需要知道您不打算修改它以允许您这样做。谢谢,我肯定需要对函数何时需要
const
参数做更多的研究