这段代码可以在我的 PC 上编译,但不能在竞赛服务器上的标准 C++98 编译器上编译
This piece of code compiles on my PC, but not on a standard C++98 compiler on a competition server
我的编译器没有出现任何错误,而且我得到了正确的结果。我也试过在线 C++98 编译器,它在那里也能正常工作。但是当我在比赛服务器上查看程序时,它说编译失败。
有人可以告诉我如何处理我的编译器或者我的代码有什么问题吗?这是程序:
#include <stdio.h>
#include <algorithm>
using namespace std;
class P
{
public:
int t;
int l;
P();
P(int t, int l);
bool operator<(P next);
};
P::P()
{
this->t = 0;
this->l = 0;
}
P::P(int x, int y)
{
this->t = x;
this->l = y;
}
bool P::operator<(P next)
{
return this->l > next.l;
}
P a[110];
int main()
{
int z, n, x, y, tim = 0;
scanf("%d %d",&z,&n);
for(int i = 0; i < z; i++)
{
scanf("%d %d",&x,&y);
P b(x,y);
a[i] = b;
}
sort(a,a + z);
tim = max(a[0].l,a[0].t);
for(int i = 1; i <= z; i++)
{
tim += a[i - 1].l - a[i].l;
tim = max(a[i].t,tim);
}
printf("%d\n",tim);
}
您的 operator<
应该是 bool operator<(const P &next) const
。
std::max
接受两个 const T&
参数。没有声明比较运算符 const
:
bool operator<(const P& next) const;
std::max
的操作数没有匹配的运算符。
关于为什么这在您的本地计算机上有效的解释之一是您对 max
的定义不是模板函数,而是一个宏,它不会有这个问题(但不是标准的C++).
我的编译器没有出现任何错误,而且我得到了正确的结果。我也试过在线 C++98 编译器,它在那里也能正常工作。但是当我在比赛服务器上查看程序时,它说编译失败。
有人可以告诉我如何处理我的编译器或者我的代码有什么问题吗?这是程序:
#include <stdio.h>
#include <algorithm>
using namespace std;
class P
{
public:
int t;
int l;
P();
P(int t, int l);
bool operator<(P next);
};
P::P()
{
this->t = 0;
this->l = 0;
}
P::P(int x, int y)
{
this->t = x;
this->l = y;
}
bool P::operator<(P next)
{
return this->l > next.l;
}
P a[110];
int main()
{
int z, n, x, y, tim = 0;
scanf("%d %d",&z,&n);
for(int i = 0; i < z; i++)
{
scanf("%d %d",&x,&y);
P b(x,y);
a[i] = b;
}
sort(a,a + z);
tim = max(a[0].l,a[0].t);
for(int i = 1; i <= z; i++)
{
tim += a[i - 1].l - a[i].l;
tim = max(a[i].t,tim);
}
printf("%d\n",tim);
}
您的 operator<
应该是 bool operator<(const P &next) const
。
std::max
接受两个 const T&
参数。没有声明比较运算符 const
:
bool operator<(const P& next) const;
std::max
的操作数没有匹配的运算符。
关于为什么这在您的本地计算机上有效的解释之一是您对 max
的定义不是模板函数,而是一个宏,它不会有这个问题(但不是标准的C++).