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]
這個超級坑爹,因為不少編譯器遇到這種問題有的還不報錯,只是有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次)
那么為什么打*呢?
因為 Linux 系統(tǒng)編譯通過!
Windows 尚未測試,有興趣的小伙伴可以自測一下然后私信,歡迎私信~~~。
(這個問題源于我自己做題時,我看標準代碼,不知為什么就是編譯不對,結(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'?
如果編譯器沒有類似提示,就仔細想想應(yīng)該是什么吧。
到此這篇關(guān)于15種 C++ 常見報錯的文章就介紹到這了,更多相關(guān)C++ 常見報錯內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
C++兩個cpp文件間如何進行各自函數(shù)的調(diào)用方式
這篇文章主要介紹了C++兩個cpp文件間如何進行各自函數(shù)的調(diào)用方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-02-02
C++中字符串全排列算法及next_permutation原理詳解
這篇文章主要為大家詳細介紹了C++中字符串全排列(遞歸法)和(迭代法)以及next_permutation底層原理,文中的示例代碼講解詳細,感興趣的可以了解一下2023-02-02

