15种 C++ 常见报错原因分析

 更新时间:2023年01月03日 14:25:20   作者:zeekliu  
这篇文章主要介绍了15种 C++ 常见报错,本文通过实例代码给大家讲解的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下

本文整合了部分 C/C++ 常见的报错原因,可根据自己的情况,使用目录跳转。

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 数组维数错误

#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  关于 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  关于 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,而且看半天才能看出来

应改为:

#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 ('}')

应改为:

#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  关于字符串的输入错误 (*)

#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
  ^

(这句话至少出现了50次)

那么为什么打*呢?

因为 Linux 系统编译通过!

Windows 尚未测试,有兴趣的小伙伴可以自测一下然后私信,欢迎私信~~~。

(这个问题源于我自己做题时,我看标准代码,不知为什么就是编译不对,结果提交以后就AC了?!)

8  写错函数 / 变量名

这个情况下,有时候编译器可能会猜测你要写的名字,比如:

#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'?

如果编译器没有类似提示,就仔细想想应该是什么吧。

到此这篇关于15种 C++ 常见报错的文章就介绍到这了,更多相关C++ 常见报错内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • C语言基于贪心算法解决装箱问题的方法

    C语言基于贪心算法解决装箱问题的方法

    这篇文章主要介绍了C语言基于贪心算法解决装箱问题的方法,简单描述了装箱问题,并结合实例形式给出了C语言使用贪心算法解决贪心问题的相关操作技巧,需要的朋友可以参考下
    2018-06-06
  • C++ TensorflowLite模型验证的过程详解

    C++ TensorflowLite模型验证的过程详解

    这篇文章给大家介绍了C++ TensorflowLite模型验证的过程,测试代码,主要是RunInference()和read_file(),详细操作过程跟随小编一起看看吧
    2021-08-08
  • C语言求两个字符串的最长公共子串

    C语言求两个字符串的最长公共子串

    这篇文章主要介绍了C语言求两个字符串的最长公共子串,实例分析了C语言操作字符串的技巧,具有一定参考借鉴价值,需要的朋友可以参考下
    2015-02-02
  • C语言图文并茂详解链接过程

    C语言图文并茂详解链接过程

    首先来思考一个问题:工程中的每个C语言源文件被编译后生成的目标文件,这些目标文件如何生成最终的可执行程序? 这就需要这节我们将要分析的链接器
    2022-04-04
  • C++两个cpp文件间如何进行各自函数的调用方式

    C++两个cpp文件间如何进行各自函数的调用方式

    这篇文章主要介绍了C++两个cpp文件间如何进行各自函数的调用方式,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2023-02-02
  • C语言线程池的常见实现方式详解

    C语言线程池的常见实现方式详解

    本文介绍了如何使用 C 语言实现一个基本的线程池,线程池的实现包括工作线程、任务队列、任务调度、线程池的初始化、任务添加、销毁等步骤,感兴趣的朋友跟随小编一起看看吧
    2025-01-01
  • c++连接mysql5.6的出错问题总结

    c++连接mysql5.6的出错问题总结

    下面小编就为大家带来一篇c++连接mysql5.6的出错问题总结。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧,祝大家游戏愉快哦
    2016-12-12
  • C++中字符串全排列算法及next_permutation原理详解

    C++中字符串全排列算法及next_permutation原理详解

    这篇文章主要为大家详细介绍了C++中字符串全排列(递归法)和(迭代法)以及next_permutation底层原理,文中的示例代码讲解详细,感兴趣的可以了解一下
    2023-02-02
  • C++适用于所有输入法的解决方案

    C++适用于所有输入法的解决方案

    这篇文章主要介绍了C++适用于所有输入法的解决方案,文中通过代码示例讲解的非常详细,对大家的学习或工作有一定的帮助,需要的朋友可以参考下
    2025-02-02
  • C++判断子序列题目详解

    C++判断子序列题目详解

    这篇文章主要为大家介绍了C++判断子序列题目,具有一定的参考价值,感兴趣的小伙伴们可以参考一下,希望能够给你带来帮助
    2021-11-11

最新评论