编译姬 / 评测姬の语言
阅读建议:开目录浏览 or
Ctrl + F
调试过程的离谱错误
无法调用函数
该函数占用空间过大。比如:在函数内声明bool vis[6e6+5]
.解决方法是把它挪到全局变量里。
神秘输出0
数组越界。Especially 倍增的第二维在循环里的时候。
编译运行错误(可见于Dev上)
y1不能做变量名!!!
[Error] array must be initialized with a brace-enclosed initializer
字面意思:数组赋值不能写"=".
char s[N][25];
bool cmp(char *x, char *y){
return strcmp(x,y)<0;
}
sort(s+1,s+n+1,cmp);
→
char s[N][25];
int cmp(const void *a, const void*b) {
char *s1 = (char *)a;
char *s2 = (char *)b;
return strcmp(s1, s2);
}
qsort(s+1,n,sizeof(char)*25,cmp);
✔
[Error] statement cannot resolve address of overloaded function
while(q4[u].size()>4) q4[u].pop_back;
调用成员函数的时候忘记加括号了捏。
while(q4[u].size()>4) q4[u].pop_back();
[Error] passing 'const Q' as 'this' argument of 'bool Q::operator<(const Q&)' discards qualifiers [-fpermissive]
错误解释:
直译:错误:将“const xxx”作为“this”参数传递会丢弃限定词。
discards qualifiers:丢弃限定符,当尝试修改const类型时,会报discards qualifiers
-fpermissive:将有关不合格代码的某些诊断从错误降级为警告。因此,使用-fpermissive将允许编译一些不合格的代码。
————————————————
版权声明:本文为CSDN博主「土豆西瓜大芝麻」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/jinking01/article/details/116214102
错误代码片段:
struct Q{
ll num;
int qid;
Q(){}
Q(ll _num,int _qid){
num=_num, qid=_qid;
}
bool operator < (const Q &x){//for set
if(num==x.num) return qid<x.qid;
return num<x.num;
}
}it;
解决方案英文版:
The error message tells you that you that you are casting of const
from your object in operator<
function. You should add const
to all member functions that don't modify member.
bool operator<(const stockType& stock) const
// ^^^^^
{
return (symbols < stock.symbols)
}
The reason why compiler complains about operator< is because std::sort uses operator< to compare the elements.
代码对照版:往bool operator < (const Q &x)
后面加了一个const
.
struct Q{
ll num;
int qid;
Q(){}
Q(ll _num,int _qid){
num=_num, qid=_qid;
}
bool operator < (const Q &x) const {//for set
if(num==x.num) return qid<x.qid;
return num<x.num;
}
}it;
[Error] non-member function 'bool operator<(const seg_tree::tree_node&, const seg_tree::tree_node&)' cannot have cv-qualifier
REASONS: use const on a non-member function
error code: friend ostream& operator<< (ostream& os, const Tree& tree) const;
right code: friend ostream& operator<< (ostream& os, const Tree& tree);
——blog
[Warning] 'sizeof' on array function parameter 'd' will return size of 'int*' [-Wsizeof-array-argument]
原理分析:
C/C++中如果一个函数接受一个数组作为参数,那么数组将会被退化为指针。
解决办法:
如果实在想用sizeof(array)/sizeof(array[0])算长度,请不要将数组作为参数,而是在main函数中算好length,再将传入函数中。
————原博客
inline void dij(int d[],int s){
memset(d,0x3f,sizeof(d));
改为
在主函数里把四个数组都初始化了,才传到函数里面。
[Error] decrement of read-only member 'std::pair<const int, int>::first'
直译:递减只读成员'std::pair<const int, int>::first'的值。
错误代码片段:
map<int,int>mp;
...
if(op==5) printf("%d\n",--mp.lower_bound(x)->first);//front
意思是运算符优先级没注意,在--mp.lower_bound(x)
两侧加上括号就可以辣。
[Error] invalid cast from type 'std::set<std::pair<int, int> >::iterator' {aka 'std::_Rb_tree_const_iterator<std::pair<int, int> >'} to type 'int'
[Error] lvalue required as left operand of assignment
lvalued 的意思为 左值;赋值语句的左边应该是变量,不能是表达式。
这种❌发生在:
#define f(o,p,q,r) f[o][p][q][r]%M
...
f(i,x,0,0)+=f(i,x-j,0,0)*f(s,j,0,1)%M;
时。检查一下是否定义时把取模也定义上了(⊙o⊙)。
[Error] unable to find string literal operator 'operator""dx1' with 'const char [13]', 'long long unsigned int' arguments
printf("%d %d %d %d\n"dx1[ye],dx1[et[i].u],dy1[et[i].v],et[i].w);
错误:没加逗号。更专业的解释.
[Error] returning a value from a constructor
直译:错误:在构造函数里返回值。
struct period {
int s,t;
ll v;
period() {} //
period(int _s,int _t,ll _v) {
return s=_s, t=_t, v=_v;
}
bool operator < (const period &y)const {
return v<y.v;
}
};
没有return
.
struct period {
...
period(int _s,int _t,ll _v) {
s=_s, t=_t, v=_v;
}
...
};
[Error] expected unqualified-id before 'long'
expected unqualified-id是GCC的默认报错。在GCC遇到语法错误又不知道报什么错的时候,基本上就会看到。
错误代码片段:
#include<bits/stdc++.h>
using namespace std;
#define ll long long
const ll M=998244353;
const int N=105;
int n,m,K,
因为是语法错误,所以我们来看看前后有没有语法错误。
#include<bits/stdc++.h>
using namespace std;
#define ll long long
const ll M=998244353;
const int N=105;
int n,m,K;//,->;,发现了
[Error] 'class std::vector<std::pair<int, int> >' has no member named 'first' + [Note] in expansion of macro 'fs'
错误代码片段:
#include<bits/stdc++.h>
using namespace std;
#define pr pair<int,int>
#define mp make_pair
#define fs first
#define sc second
vector< pr >us;
...
for(int i=0;i<sz;i++) printf("%d %d",us.fs,us.sc);
vector容器存放的是只有一种类型的数据。在C++的常见容器中,只有map/multimap支持键值对存放形式。
意思是vector
里面不能存node
,别做梦了洗洗睡吧(
解决方法:
int op[N][2],u;
[Error] 'class std::vector' has no member named 'no'
struct ope{
int cnt,no;
...
void ins(int &p,int lb,int rb,int z,int tg){
...
vector< ope >op[N];
...
for(int i=0;i<sz;i++) st.ins(rt,1,N,op[u].no,op[u].cnt);
[Error] cannot convert 'std::vector' to 'ope'
for(int i=0;i<sz;i++) st.ins(rt,1,N,op[u]);
vector< ope > ::iterator it;
for(it=op[u].begin(); it!=op[u].end(); it++) st.ins(rt,1,N,*it);
done!
[Error] ld returned 1 exit status
字面意思:ld文件返回了一个存在的状态,就是说你上次运行的.exe
还没运行完。一般是因为没有输入、输出太大太慢或陷入死循环。对于后两种情况,可以用cmd命令强制终止:
taskkill /f /im abaaba.exe /t
或者可以按F9,有时候Dev会给你把窗口吐出来。
Failed to execute".cpp address": Error 0: 操作成功完成。
解决方法
- 更换GCC(在DEV-C++编译界面右上角)
在此提一下GCC的Release/Debug/Profiling之间的区别与联系:
GCC(GNU Compiler Collection,GNU 编译器套装),是一套由 GNU 开发的编程语言编译器。它是一套以 GPL 及 LGPL 许可证所发布的自由软件,也是 GNU 计划的关键部分,亦是自由的类 Unix 及苹果计算机 Mac OSX 操作系统的标准编译器。GCC(特别是其中的 C 语言编译器)也常被认为是跨平台编译器的事实标准。
——CSDN博主「—叶丶知秋」
debug 调试,可以对软件进行单步执行、堆栈跟踪、调试等操作来发现bug
release 发行版,如果最终调试后程序没有明显bug,可以作为可用的软件分享给他人使用就可以使用这个选项编译。
profiling 性能分析。可以对软件执行过程中的cpu利用率,内存占有进行分析。也可以用来发现、分析异常、bug。
——百度知道答主 wchyumo2011
Debug:Debug 通常称为调试版本,通过一系列编译选项的配合,编译的结果通常包含调试信息,而且不做任何优化,以为开发人员提供 强大的应用程序调试能力。
Release:Release通常称为发布版本,是为用户使用的,一般客户不允许在发布版本上进行调试。所以不保存调试信息,同时,它往往进行了各种优化,以期达到代码最小和速度最优。为用户的使用提供便利。
——CSDN博主 —叶丶知秋
一言以蔽之,我们一般用的是Release。
- 杀毒软件
我什么都没干,直接重启了电脑,又可以了……学校电脑抽风罢……
Error: value of 0000000xxxxxxx too large for field of 4 bytes at 0000000xxxxxxx
全局区声明变量占用空间过大。意思是您的数组要开小一点。
[Warning] extended initializer lists only available with -std=c++11 or -std=gnu++11
问题不大,评测姬能给过。一般出现在你使用了不是这个时代的语法的时候,例如对于set< pair<ull,ull> > s;
,写s.insert({a,b});
。
[Error] cannot pass objects of non-trivially-copyable type 'std::string {aka class std::basic_string}' through '...'
报错姬告诉你,不应该用scanf
或printf
读入或输出一个字符串(string
)。
你可以改用巨慢的cin
、cout
,也可以把string
换成char s[len]
。直接快读在没有EOF
的时候也挺香。
[Error] declaration does not declare anything [-fpermissive] 和 [Error] wrong number of template arguments (1, should be 2)
原因是写了这个:
#define db double
#define pr pair<db,int>
...
for(int i,a,b,db c;i<=m;i++){
...
最初迫于报错,把define
片段改成了typedef double db
,然后报错就挪到了for
循环的地方。
把a
,b
,c
改到在循环里定义了,就很好。问题解决。
[Warning] non-static data member initializers only available with -std=c++11 or -std=gnu++11
意思是你在结构体里面,初始化了不是常数的变量。而这种操作只能在C++11(及以上?)版本里用。
任何简单的声明都是允许的,除了
不允许使用 extern和register存储类说明符;
不允许使用thread_local存储类说明符(但允许用于静态数据成员)(C++11 起)
不允许不完整的类型、抽象类类型及其数组:特别是,一个类C不能有类型的非静态数据成员C,尽管它可以有类型的非静态数据成员C&(对 C 的引用)或C*(指针到 C);
如果至少存在一个用户声明的构造函数,则非静态数据成员不能与类名同名;
说明auto符不能用于非静态数据成员声明(尽管它允许用于在类定义中初始化的静态数据成员)(C++11 起).
此外,允许 位域声明。
[Warning] suggest parentheses around comparison in operand of '&' [-Wparentheses]
该警告希望你在&(按位与)表达式左右加上括号。
[Warning] left shift count >= width of type
左移位数太多(值太大了)。
Accepted
Wrong Answer
当你觉得自己当算法没有错但总有部分数据WA时……
- 大样例过不了
建议:Ⅰlong long! ;Ⅱ 优化算法(卡常?),不该干的事不要干。 - 被HACK
建议:再次仔细检查代码,寻找漏洞。 - 输出0,或千辛万苦终输出0
你是不是把double
数组memset
为了?
当本地输出与评测时输出不一致时……
- 可能是内存的问题
比如你用了decltype
来开二维数组,以树形背包为例:
其实你可以:
vector< int >dp[N];
加上
for(int i=0;i<=n;i++) dp[i].resize(wt+1);
Time Limit Exceeded
你谷的TLE显示的秒数是的。
如果发现某样例,恭喜你,吸氧无用。你需要换上时间复杂度正确的算法。
Memory Limit Exceeded
Runtime Error(良心的LOJ评测会提示,正赛RE就0辣)
频繁RE
- 是不是无用的STL开多了
- 双向边
- 链星
head
数组memset
了没?ini()
全了吗?
Exit code: 8
Floating point exception,浮点格式异常。发现它的时候请自我反省,有没有字符串哈希,有没有用unsigned long long
。
这种错误可能是/0
、%0
、或unsigned long long
溢出导致的/0
或%0
导致的。
我的解决方法是:
- 用前缀的形式存储字符串的hash值
- 用质数来做进制哈希!如果是字符串,最好用131,瞎用什么31就会很容易哈希冲突。
- (与这种错误无关,但是与字符串哈希有关)对精度高要求,少用除法和取模,多用加减法和乘法。
Exit code: 11
访问越界/无意义运算
一般是语法上没问题,但是运行的时候有问题的情况,
例如拿一个数除以一个0,就会出现runtime error。
我估计跟你建立的数组大小关系不大,而是你非法调用了不在你数组范围内的数,
例如 明明数组size是5, 你却把值写到了 array [5] (超出范围)里面,类似这种,有可能发生runtime error
建议检查一下你的 for 或者 while loop~~
————asdsb000 ZOL问答
友情提示:当所开数组过大(但没报错)的时候,测大样例时会部分RE~ 这时之前就该检查一下空间辽……
REWA
一般在有大样例时出现,思路没有问题。对算法再进行时空上的小优化即可顺利AC。
Warning: control reaches end of non-void function
/sandbox/source/main.cpp: In function ‘bool dfs(int, int, int)’:
/sandbox/source/main.cpp:34:1: warning: control reaches end of non-void function [-Wreturn-type]
34 | }
| ^
本应带有返回值的函数可能最终没有返回值。(咕咕)
一些精心设计的样例可能刚好测不出这个问题╮(╯▽╰)╭
加上相应的return
就好啦
Idleness limit exceeded
直译:懒惰超过限制。
意译:CF交互题:你需要刷新缓冲区。
iostream
的输出语句自带flush,而cstdio
的没有.所以建议使用cin
cout
.
After printing a query, don't forget to output line feed and flush the output buffer. Otherwise you will get the verdict
Idleness limit exceeded
. To flush the buffer, use:
fflush(stdout) or cout.flush() in C++;
System.out.flush() in Java;
flush(output) in Pascal;
stdout.flush() in Python;
Read documentation for other languages.
比如这样:
cout<<"! "<<*s.begin()<<" "<<*s.rbegin()<<endl;
//fflush(stdout);
写fflush(stdout);
就报错,写cout
就没事。
wrong output format Unexpected end of file - double expected
打暴力也要看题!!题目让你输出 个数你输出了啥!
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 单元测试从入门到精通
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 上周热点回顾(3.3-3.9)
· winform 绘制太阳,地球,月球 运作规律