15種?C++?常見報錯原因分析
本文整合了部分 C/C++ 常見的報錯原因,可根據(jù)自己的情況,使用目錄跳轉(zhuǎn)。
1 重定義變量
#include<bits/stdc++.h>
using namespace std;
int main()
{
int a;
cin>>a;
int a;
cout<<a<<endl;
}Error:redefinition of 'a'
改為:
#include<bits/stdc++.h>
using namespace std;
int main()
{
int a;
cin>>a;
cout<<a<<endl;
}2 缺少分號
#include<bits/stdc++.h>
using namespace std;
int main()
{
int a;
cout<<a<<endl
}Error:expected ';' after expression
改為:
#include<bits/stdc++.h>
using namespace std;
int main()
{
int a;
cout<<a<<endl;
}3 數(shù)組維數(shù)錯誤
#include<bits/stdc++.h>
using namespace std;
int main()
{
int a[101][101];
a[0]=1;
cout<<a[0]<<endl;;
}Error:array type 'int [101]' is not assignable
改為:
#include<bits/stdc++.h>
using namespace std;
int main()
{
int a[101];
a[0]=1;
cout<<a[0]<<endl;;
}4 關(guān)于 if 與 else
#include<bits/stdc++.h>
using namespace std;
int main()
{
int a;
cin>>a;
if (a==1;) a=2;
}Error:expected expression
Warning: equality comparison result unused [-Wunused-comparison]
if 判斷里不能有分號!
改為:
#include<bits/stdc++.h>
using namespace std;
int main()
{
int a;
cin>>a;
if (a==1) a=2;
}5 關(guān)于 if 與 else
#include<bits/stdc++.h>
using namespace std;
int main()
{
int a;
cin>>a;
if (a=1) a=2;
}這個是把等號寫成了賦值號
Warning: using the result of an assignment as a condition without parentheses [-Wparentheses]
這個超級坑爹,因?yàn)椴簧倬幾g器遇到這種問題有的還不報錯,只是有Warning,而且看半天才能看出來
應(yīng)改為:
#include<bits/stdc++.h>
using namespace std;
int main()
{
int a;
cin>>a;
if (a==1) a=2;
}6 括號匹配錯誤
#include<bits/stdc++.h>
using namespace std;
int main()
{
int a[10];
a[1=(a[1+1)*1);
}
}Error: expected ']'
Error: expected ']'
Error: extraneous closing brace ('}')
應(yīng)改為:
#include <bits/stdc++.h>
using namespace std;
char c[101];
int main()
{
ios::sync_with_stdio(0);
cin.tie(0);
cin>>c+1;
return 0;
}===========Upd: 22-05-19============
7 關(guān)于字符串的輸入錯誤 (*)
#include <bits/stdc++.h>
using namespace std;
char c[101];
int main()
{
ios::sync_with_stdio(0);
cin.tie(0);
cin>>c+1;
return 0;
}(MacOS??????)
Error: invalid operands to binary expression ('std::istream' (aka 'basic_istream<char>') and 'char *')
cin>>c+1;
~~~^ ~~~
Warning: operator '>>' has lower precedence than '+'; '+' will be evaluated first [-Wshift-op-parentheses]
cin>>c+1;
~~~^~
和一堆 note:
Note: candidate function template not viable: no known conversion from 'std::istream' (aka 'basic_istream<char>') to 'std::byte' for 1st argument
operator>> (byte __lhs, _Integer __shift) noexcept
^
(這句話至少出現(xiàn)了50次)
那么為什么打*呢?
因?yàn)?Linux 系統(tǒng)編譯通過!
Windows 尚未測試,有興趣的小伙伴可以自測一下然后私信,歡迎私信~~~。
(這個問題源于我自己做題時,我看標(biāo)準(zhǔn)代碼,不知為什么就是編譯不對,結(jié)果提交以后就AC了??。?/p>
8 寫錯函數(shù) / 變量名
這個情況下,有時候編譯器可能會猜測你要寫的名字,比如:
#include <bits/stdc++.h>
using namespace std;
int main()
{
ios::sync_with_stdio(0);
cin.tie(0);
int a=1,b=2;
mam(a,b);
return 0;
}Error: use of undeclared identifier 'mam'; did you mean 'max'?
如果編譯器沒有類似提示,就仔細(xì)想想應(yīng)該是什么吧。
到此這篇關(guān)于15種 C++ 常見報錯的文章就介紹到這了,更多相關(guān)C++ 常見報錯內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
QT中進(jìn)程的創(chuàng)建實(shí)現(xiàn)
本文主要介紹了QT中進(jìn)程的創(chuàng)建實(shí)現(xiàn),詳細(xì)介紹了創(chuàng)建進(jìn)程的整個過程,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2023-08-08
Clion2020.2.x最新激活碼破解版附安裝教程(Mac Linux Windows)
Clion2020增加了很多新特性,修復(fù)了大量bug,大大提高了開發(fā)效率。這篇文章主要介紹了Clion2020.2.x最新激活碼破解版附安裝教程(Mac Linux Windows),需要的朋友可以參考下2020-11-11
OpenCV透視變換應(yīng)用之書本視圖矯正+廣告屏幕切換
透視變換是指利用透視中心、像點(diǎn)、目標(biāo)點(diǎn)三點(diǎn)共線的條件,按透視旋轉(zhuǎn)定律使承影面繞跡線旋轉(zhuǎn)某一角度,破壞原有的投影光線束,仍能保持承影面上投影幾何圖形不變的變換。本文將為大家介紹兩個OpenCV透視變換應(yīng)用,需要的可以參考一下2022-08-08
C++輕量級界面開發(fā)框架ImGUI介紹小結(jié)
如果從事過C++?Windows客戶端開發(fā),大家對MFC、Qt、DuiLib等各種DirectUI應(yīng)該有了解,本篇給大家介紹一個超級輕量級的C++開源跨平臺圖形界面框架ImGUI,感興趣的可以了解一下2021-11-11
C語言 鏈?zhǔn)蕉鏄浣Y(jié)構(gòu)詳解原理
二叉樹的鏈?zhǔn)酱鎯Y(jié)構(gòu)是指,用鏈表來表示一棵二叉樹,即用鏈來指示元素的邏輯關(guān)系。通常的方法是鏈表中每個結(jié)點(diǎn)由三個域組成,數(shù)據(jù)域和左右指針域,左右指針分別用來給出該結(jié)點(diǎn)左孩子和右孩子所在的鏈結(jié)點(diǎn)的存儲地址2021-11-11
C++實(shí)現(xiàn)LeetCode(38.計(jì)數(shù)和讀法)
這篇文章主要介紹了C++實(shí)現(xiàn)LeetCode(38.計(jì)數(shù)和讀法),本篇文章通過簡要的案例,講解了該項(xiàng)技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下2021-07-07

