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

目录
  • 1 重定义变量
  • 2  缺少分号
  • 3 数组维数错误
  • 4  关于 if 与 else
  • 5  关于 if 与 else
  • 6  括号匹配错误
  • 7  关于字符串的输入错误 (*)
  • 8  写错函数 / 变量名

本文整合了部分 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++ 常见报错内容请搜索我们以前的文章或继续浏览下面的相关文章希望大家以后多多支持我们!

(0)

相关推荐

  • C++ OpenCV裁剪图片时发生报错的解决方式

    目录 从相机获取图像时直接处理会报错,读取本地视频不会报错 对本地视频进行裁剪不会报错,对相机实时获取的帧进行裁剪会报错 总结 从相机获取图像时直接处理会报错,读取本地视频不会报错 原代码 g_mvCamera.GetImage(m_matCameraFrame); //开始OCR OCRImg(); 将从相机捕获的帧直接imwrite进本地,再在OCR中imread读取本地路径下的图片,报错消失 g_mvCamera.GetImage(m_matCameraFrame); imwrite("i

  • Python3安装模块报错Microsoft Visual C++ 14.0 is required的解决方法

    问题一:安装模块时出现报错 Microsoft Visual C++ 14.0 is required,也下载安装了运行库依然还是这个错误 解决: 1.打开Unofficial Windows Binaries for Python Extension Packages(http://www.lfd.uci.edu/~gohlke/pythonlibs/),这里面有很多封装好的Python模块的运行环境 2.找到所需要下载的模块文件对应版本进行下载. 如,需要下载Pymssql,本机安装是32位

  • C或C++报错:ld returned 1 exit status报错的原因及解决方法

    目录 参考资料:关于Id returned 1exit status的解决办法 一.问题描述 二.个人解决 三.总结反思 C或C++报错:ld returned 1 exit status(ld返回1,退出状态) 可能是以下原因:        1)程序正在运行,无法编译,上次运行的窗口未关闭. 程序窗口重复运行没有及时关闭,存在多个打开窗口,得一个个都关闭了再编译. 2)一个项目中有多个 xx.c 文件,将多余的xx.c文件改成头文件即可. 3)有函数拼写错误,如:printf拼写成prntf

  • 如何基于C++解决RTSP取流报错问题

    使用g++ opencv_demo.cpp -o test 会报以下错误 这是我的代码: #include <string> #include <iostream> #include <time.h> #include <opencv2/highgui/highgui.hpp> #include <opencv2/opencv.hpp> #include <opencv2/core.hpp> #include <opencv2/

  • 解决pip install dlib报错C++11 is required to use dlib

    目录 1.错误原因 2.原因分析 3.解决办法 1.错误原因 在使用pip install dlib安装dlib的时候报错, 错误的详细信息如下: ERROR: Command errored out with exit status 1:command: /root/miniconda3/envs/cv_1/bin/python -u -c ‘import sys, setuptools, tokenize; sys.argv[0] = ‘"’"’/tmp/pip-install-j

  • c++报错问题解决方案lvalue required as left operand of assignment

    在编程时出现报错: lvalue required as left operand of assignment 出现此错误原因,是因为,等号左边是不可被修改的表达式或常量.而表达式或常量不能作为左值.归根结底类似于 3=b; 这种错误.而查看代码发现,是判断出了问题 if(!strA.compare(strB)&&!strC.compare(strD)&&n1=n2){     ... } 由于n1==n2漏写一个等于号,造成括号内由判断条件变成了赋值语句:左值=n2.而由

  • 解决安装mysqlclient的时候出现Microsoft Visual C++ 14.0 is required报错

    在安装mysqlclient的时候出现了以下报错: 解决办法: 1.到提示网址:https://visualstudio.microsoft.com/download/里面下载对应VC++版本安装后继续安装mysqlclient.但是本人没有找到对应文件,故不再过多赘述,这里说下第二种方法. 2.1到https://www.lfd.uci.edu/~gohlke/pythonlibs/找到mysqlclient编译包下载对应版本: 注:前面代表python版本,后面的win代表Python位数.

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

    目录 1 重定义变量 2  缺少分号 3 数组维数错误 4  关于 if 与 else 5  关于 if 与 else 6  括号匹配错误 7  关于字符串的输入错误 (*) 8  写错函数 / 变量名 本文整合了部分 C/C++ 常见的报错原因,可根据自己的情况,使用目录跳转. 1 重定义变量 #include<bits/stdc++.h> using namespace std; int main() { int a; cin>>a; int a; cout<<a&

  • C语言中pow函数使用方法、注意事项以及常见报错原因

    目录 1.首先使用pow函数必须要加头文件 : 2.pow()用来计算以x 为底的 y 次方值,然后将结果返回. 3.注意可能引起报错的原因 可能导致错误的情况: 总结 1.首先使用pow函数必须要加头文件 : #include<math.h> pow() 函数用来求 x 的 y 次幂(次方),x.y及函数值实际上为double型 ,其在使用中的原型为:double pow(double x, double y); 注意,在某些特定的情况之下,pow函数的double类型可能会引起输出结果的错

  • Go语言将string解析为time.Time时两种常见报错

    目录 1.错误 2.报错信息详细 3.解决方案 1.错误 错误1:parsing time “xx”: xxx out of range错误2:parsing time “xx”:cannot parse"xx" as “-” 2.报错信息详细 详细1: parsing time "2022/10/31 19:00:01": month out of range 详细2: parsing time "2022/10/31 20:00:01" as

  • Java @Autowired报错原因分析和4种解决方案

    目录 报错原因分析 解决方案1:关闭报警机制 解决方案2:添加Spring注解 解决方案3:允许注入对象为NULL 解决方案4:使用@Resource注解 总结 前言: 上图的报错信息相信大部分程序员都遇到过,奇怪的是虽然代码报错,但丝毫不影响程序的正常执行,也就是虽然编译器 IDEA 报错,但程序却能正常的执行,那这其中的原因又是为何? 报错原因分析 报错的原因首先是因为 IDEA 强大的报警机制,@Autowired 为 Spring 的注解,含义是将某类动态的注入到当前类中, 如下图所示:

  • Xcode8下iOS10常见报错闪退,字体适配和编译不过的问题及解决方案

    9月14日凌晨1点,苹果推送了iOS10,于是一上班就迅速升级了iOS10,然后坑就这样开始了... 问题1 首先是xcode的问题,发现xcode升级到8才能真机运行,于是先了解了下iOS10的适配. 有这个iOS10适配总结,还有这个iOS10适配问题收集整理,还有这个iOS10适配,还有很多其他的. 这个好办,取消nullabl关键字就好. 然后另一个蛋疼的问题来了 问题二,编译不过的问题 蛋疼的clang报错le.. 这个是详细的信息,一堆莫名其妙的东西出来了. 隐隐约约感觉是WGS84

  • Python中如何处理常见报错

    1.首先是常见的语法错误.2.然后是基础语法中的常见异常.3.最后是操作文件过程中的常见异常,这部分也是重难点知识. 这是我们在终端比较常见的报错信息: 按照 Python 官方文档的定义,我们在终端见到的“错误消息”至少可以被分为两类:语法错误(syntax errors)和异常(exceptions) . 语法错误(syntax errors)是初学者最容易犯的错误,简单来说就是代码不符合 Python 的基本语法规范而导致程序出了问题. 当你的代码完全符合 Python 的语法规范后,就该

  • Python常见报错解决方案总结(新手拯救指南)

    目录 前言 01缩进错误(IndentationError) 02Tab 和空格混用(TabError) 03语法错误(SyntaxError) 04变量名错误(NameErro) 05索引错误(IndexError) 06键错误(KeyError) 07类型错误(TypeError) 08属性错误(AttributeError) 总结 前言 如果说写代码最害怕什么,那无疑是Bug.而对于新手来说,刚刚接触编程,在享受写代码的成就感时,往往也会被各式各样的Bug弄得晕头转向.今天,我们就做了一期

  • Vue常见报错以及解决方案实例总结

    目录 前言 一.报错结构 二.常见问题总结及解决方法 Mixed spaces and tabs Element is missing end tag TypeError: Cannot read properties of undefined (reading '...') TypeError: ...forEach is not a function '...' is not defined / no-undef 总结 前言 写代码的过程中一定会遇到报错,遇到报错不要担心,认真分析就可以解决

  • R语言初学者的一些常见报错指南

    目录 前言 第一类:工作路径问题 未设定工作路径 当前路径需要修改 第二类:对象名或函数名问题 未找到函数名报错 函数名大小写问题 未找到赋值对象 对象赋值不规范 第三类:符号问题 中文逗号报错 绝对路径的设定符号使用不规范 缺少括号或引号 赋值号报错 必要的引号与括号 第四类:中文注释乱码 第五类:数据集或变量长度不同 总结 前言 与Python.C语言等相比,R语言可以说是比较容易的编程语言之一(更适合数据探索和科研).尽管R语言相对简单,但仍给新手小白们带来无数的困难和痛苦.特别是,当你在

  • npm install常见报错以及问题详解

    目录 前言 一.ERESOLVE unable to resolve dependency tree 1.可能性一:镜像源无法访问 2.可能性二:npm版本过低或者过高 3.可能性三:node和npm版本不匹配 二.Error: EACCES: permission denied, mkdir ‘/usr/local/lib/node_modules/yarn’ 三.Error: Can’t find Python executable “python”, you can set the PYT

随机推荐