C++ 编程常见错误收集

C++模板编程常见错误及调试方法 C++模板编程提供了强大的功能,但也伴随着一些常见的错误和调试难题。理解常见的模板错误原因(如实例化失败、SFINAE、递归问题等)以及调试技巧(如静态断言、类型特性、模板拆分等),可以帮助开发者更有效地进行模板编程。通过不断调试和优化,能够克服模板编程中的挑战,编写出更加健壮和高效的模板代码。 阅读详情

It is an error to modify a const variable. For example, after

const double pi = 3.141592654;

it is illegal to change the value of pi:

pi = 3.0 ; //ERROR


The definition of p

int a[] = {-1, -2 , -3};

const int* p = a;

promises not to change what p points to, although the value of  p itself can be changed. Thus

p[0] += 2; //ERROR

is an error, but

int b[4];

p = b; //OK

is legal.


The definition of p

int a[] = {-1, -2, -3};

int* const p = a;

promises not to change the value of p, although the value of what p points to can be changed. Thus

int b[] = {1, 2, 3, 4};

p = b; //ERROR

is an error, but

p[2]++; //OK

is legal.


It is legal to change the value of a const parameter:

void f (const int i)

{

//?o:p>

i = 8; //ERROR

//?o:p>

}


It is illegal to change the value to which a const pointer parameter points:

void f (const float* p)

{

//?o:p>

p[1] *= 2.0; //ERROR

//?o:p>

}


It is illegal to invoke an undeclared function:

#include <iostream.h>

#include <stdlib.h>

 

int main()

{

int i = 1, j = 3;

//ERROR: swap not declared

swap(i, j);

cout << 搃 =?<<  i << endl;

 

return EXIT_SUCCESS;

}

 

void swap(int& a, int& b)

{

int t;

t = a;

a = b;

b = t;

}

 

This error can be corrected by defining, and implicitly declaring , swap before main

 

#include <iostream.h>

#include <stdlib.h>

 

void swap(int& a, int& b)

{

int t;

t = a;

a = b;

b = t;

}

 

int main()

{

int i = 1, j = 3;

swap(i, j);

cout << 搃 =?<<  i << endl;

 

return EXIT_SUCCESS;

}

 

or declaring swap before main

 

#include <iostream.h>

#include <stdlib.h>

 

void swap(int& a, int& b);

 

int main()

{

int i = 1, j = 3;

swap(i, j);

cout << 搃 =?<<  i << endl;

 

return EXIT_SUCCESS;

}

 

void swap(int& a, int& b)

{

int t;

t = a;

a = b;

b = t;

}

 

or declaring swap in main

 

#include <iostream.h>

#include <stdlib.h>

 

int main()

{

void swap(int& a, int& b);

int i = 1, j = 3;

swap(i, j);

cout << 搃 =?<<  i << endl;

 

return EXIT_SUCCESS;

}

 

void swap(int& a, int& b)

{

int t;

t = a;

a = b;

b = t;

}


Since it is illegal to invoke an undeclared function, each system function must be declared. Typically, system functions are declared by including system header files such as stdio.h and stdlib.h, which contain the required function declarations. Also, all system macros must be defined by including the appropriate header files. Thus

int main()

{

// ERROR

printf(there looking at you kid./n?;

//ERROR

return EXIT_SUCCESS;

}

is illegal since printf is not declared and EXIT_SUCCESS is not defined. These errors can be corrected by including stdio.h, which contains a declaration of printf, and stdlib.h, which contains the definition of EXIT_SUCCESS:

#include <stdio.h>

#include <stdlib.h>

 

int main()

{

printf(there looking at you kid./n?;

return EXIT_SUCCESS;

}


It is an error to interpret the function declaration

int f();

as giving no information about for parameters. In fact, the declaration states that f has no parameters. For this reason, the following is an error;

int f();

 

int main()

{

//?o:p>

i = f(6);

//?o:p>

}


The only definitions of main that are guaranteed to be portable are

 

int main()

{

//?o:p>

}

 

and

 

int main( int argc, char* argv[] )

{

//?o:p>

}

 

Thus definitions such as

 

void main()

{

//ERROR

}

 

may not work in some implementations.


Explicit dereference is not used when an argument is passed by reference. For this reason, the following is an error;

void swap( int& a, int& b )

{

int t

//ERROR : a and b are not pointers

t = *a;

*a = *b;

*b = t;

}

 

Similarly the following is illegal:

struct string {

char s[80];

};

 

void copy( string& str, char* t )

{

// ERROR

strcpy( str->s, t);

}

The error can be corrected by changing the offending line to

strcpy(str.s, t);


The keyword inline can be used to request that a function be expanded inline? that is, that each occurrence of a call of the fuction be replaced with the code that implements the function. The compiler, for various reasons, may not be able to honor the request. The situation is analogous to a macro expansion. When the preprocessor expands a macro, it replaces each occurrence of the macro with the macro definition. When a macro is expanded or when a function is expanded inline and the program is run, the overhead of a function call is avoided so that the program may execute more efficiently. A disadvantage of the macros and inline functions is that if the expansions are large or there are many expansions, the size of the executable image becomes quite large. Unlike a macro that is expanded by the preprocessor, an inline function is expanded (i.e. translated) by the compiler.

 

An inline function is visible only from the point at which it is declared to the end of the file. For this reason the following is an error:

#include <iostream.h>

#include <stdlib.h>

 

int main()

{

int i = 1, j = 3;

swap(i, j); //ERROR

cout << “i =“<<  i << endl;

 

return EXIT_SUCCESS;

}

 

inline swap(int& a, int& b)

{

int t;

t = a;

a = b;

b = t;

}

 

The error can be corrected by defining, and implicitly declaring, swap before main.

 

#include <iostream.h>

#include <stdlib.h>

 

inline swap(int& a, int& b)

{

int t;

t = a;

a = b;

b = t;

}

 

int main()

{

int i = 1, j = 3;

swap(i, j);

cout << “i =“<<  i << endl;

 

return EXIT_SUCCESS;

}

 

or declaring swap before main

 

#include <iostream.h>

#include <stdlib.h>

 

inline swap(int& a, int& b);

 

int main()

{

int i = 1, j = 3;

swap(i, j);

cout << “i =“<<  i << endl;

 

return EXIT_SUCCESS;

}

 

inline swap(int& a, int& b)

{

int t;

t = a;

a = b;

b = t;

}

 

or declaring swap in main

 

#include <iostream.h>

#include <stdlib.h>

 

int main()

{

inline swap(int& a, int& b);

int i = 1, j = 3;

swap(i, j);

cout << “i =“<<  i << endl;

 

return EXIT_SUCCESS;

}

 

inline swap(int& a, int& b)

{

int t;

t = a;

a = b;

b = t;

}


All values without default values must come first in the parameter list and then be followed by all the parameters with default values. For this reason, the following is an illegal function header:

// ERROR

int f( float x = 1.3, int i, char c=)
The error can be corrected by omitting the deafult value for x or by adding a default value for i.


If a parameter does not have a default value, an argument  must be supplied when the function is invoked. For example, if the header of f is ;

int f ( float x, int i, char c);

legal invocations of f are

//LEGAL

f (93.6, 0, 慭t?;

f(93.6, 0);

but

//ERROR

f( 93.6);

is illegal.


To free a number of contigious cells allocated by new, use delete[], not delete. As examples,

float_ptr = new float[100];

delete float_ptr; //ERROR

delete[] float_ptr; // CORRECT


Do not use new and delete with C storage management functions:

float_ptr = new float[100];

free( float_ptr ); //BAD LUCK


The right shift operator >> is overloaded for input:

cin >> x; //CORRECT

cin << x; //ERROR


The left shift operator << is overloaded for output:

cout << x; //CORRECT

cout >> x; //ERROR


To use the C++ classes and predefined objects for input and output, the file iostream.h must be included:

//ERROR

#include <stdio.h>

#include <stdlib.h>

 

int main()

{

cout << 揋o Cubs!?<< endl;

 

return EXIT_SUCCESS;

}


The method get, when used in the form,

cin.get( buff, max_line );

does not store the terminating new line and it does not remove it from the standard input. For this reason, the following is an infinite loop:

char buff[80];

//ERROR infinite loop

while( cin.get( buff, 80 ))

            cout << buff << endl;


After a manipulator is placed into the stream, all subsequent input or output is formatted accordingly except for the field width, which reverts to zero after a string or number is printed. Do not assume that at the end of a statement, all input/output reverts to default settings. For example, in the following code, the input is written in decimal, then in hexadecimal twice:

int n = 100;

cout << n << endl;

cout << hex << n << endl;

cout << n << endl; // still hex


Mixing the C and C++ input/output facilities may produce unexpected results unless the function

ios::sync_with_stdio

is invoked. As examples, the code

#include <stdio.h>

#include <iostream.h>

#include <stdlib.h>

 

int main()

{

int a = 2, b = 5;

//RISKY

printf(?d ? a);

cout << b << endl;

return EXIT_SUCCESS;

}

 

is risky, but

 

#include <stdio.h>

#include <iostream.h>

#include <stdlib.h>

 

int main()

{

int a = 2, b = 5;

//OK

ios::sync_with_stdio();

printf(?d ? a);

cout << b << endl;

return EXIT_SUCCESS;

}

明天继续.....

 

 

关于在main.cpp文件中包含另一个.cpp文件的错误及原因 今天打开自己以前写代码突然觉得在man函数中如果要用的很多自己实现的函数,如果把它们都放在main.cpp文件中太臃肿了。调试起来特别不方便。能不能把这些自己实现的函数放到另外一个文件中呢? 可是又不能把这些函数放在.h文件中。因为书上说.h文件中做好只放结构体、类等的申明,函数实现最好放在.cpp文件中。好吧那我就把这些函数放到一个.cpp文件中吧。所以我就新建了一个... 阅读详情

相关推荐

C++语言常见错误分析汇总

连接错误:不能打开P1.exe文件,以改写内容。一般是P1.Exe还在运行,未关闭,关闭运行界面即可。这个就是说,你的main函数重定义了。你看看是不是你的工程里面,包含了很多个有main函数的文件?连接错误:不能打开P1.exe文件,以改写内容。成员函数“OnTimer”没有在“CHelloView”中声明。重载的函数“void reset(int)”在类“B”中找不到。“CMyApp::InitInstance”函数必须返回一个值。类B对类A中同名函数f1的重载仅根据返回值或调用约定上的区别。

技术日常 1847

《C++编程调试秘笈》导读

前言 C++编程调试秘笈敏锐的读者可能会根据本书的英文书名《Safe C++》推断出C++编程语言多少是有点儿不安全的。这确实是很灵敏的感觉!并且非常正确。C++语言可能导致程序员出现所有类型的错误,例如访问一个动态分配的数组边界之外的内存,或者从那些从未初始化过的内存进行读取,或者分配了内存但忘了销毁它。简而言之,程序员在使用C++进行编程的时...

weixin_34092455的博客 98

C/C++编程中容易犯的10种初级编程错误

根据实际工作中碰到的问题,列举出C/C++编程新手容易犯的10种编程错误。

dvlinker的技术专栏 8901

21天学通C++第八章——指针

即如何理解,当ptr指向new1,使用完成以后未使用delete对new1所占用的空间释放,然后又将ptr=new2,则会产生内存泄漏,即new1未被释放的同时也没有指针指向,即产生内存泄漏。在使用完new申请内存之后,没有配套的delete,则之后会产生内存泄漏。(1)初始化指针变量,若不初始化则可能不具有访问权限,产生冲突。占用内存越多,系统运行越慢,内存泄漏会占用宝贵的内存资源。ptr中必须存放的是有效的内存地址,即*ptr为有效的数值。即在delete之后ptr变得无效,则就不能再使用它了。

weixin_66405733的博客 1484

54、C++编程中的常见错误及避免

本文详细探讨了C++编程中常见的错误类型及其避免方法,包括语法错误、逻辑错误、内存管理错误等,并提供了相应的示例代码。同时,文章还介绍了调试技巧、代码规范和最佳实践,以及如何避免高级错误,帮助开发者编写高质量、可靠的C++代码。

metaverse5vr的博客 1118

C/C++编程常见错误及解决方案大全

htmltable {th, td {th {pre {简介:在C/C++编程中,由于语言的强类型和灵活性,初学者常会遇到各种编程错误。本文总结了18个常见错误,包括类型不匹配、数组越界、空指针解引用、内存泄漏、悬挂指针等,并提供了解决方案和最佳实践,以帮助开发者提升编程技能和避免这些错误。

weixin_33597080的博客 1204

C++编程常见错误及处理

C++编程常见错误及处理。在 C++ 程序错误一般分类:语法错误;运行错误;语义错误(也称逻辑错误)。本文介绍相关错误产生的原因及处理

cnds123的专栏 1万+

C++编程常见错误及IDE环境使用指南

本文针对C++编程中的常见错误进行了详细解析,并通过Turbo C++编译器的具体示例进行了说明。同时,介绍了在Turbo C++环境下创建和执行C++程序的基本步骤,以及如何在IDE环境下进行程序的编写和调试。

weixin_42579969的博客 601

C/C++一些常见的错误

comparison between pointer and integer :比较了两种不同类型的变量,指针和整型

默的博客 3万+

如何在C++编程中避免常见错误

本书通过展示111个有错误的程序和3个正常工作的程序,旨在教导读者如何识别和修正C++编程中的常见错误。作者Steve Oualline通过亲身经历和朋友的案例,提供了一种独特的学习方式,让读者通过观察和分析错误来掌握正确的编程实践。书中不仅讨论了编译器无法捕捉的错误,还提供了提示和答案来帮助读者更好地理解这些错误。这本实用的书籍献给所有在日常工作中与复杂、充满错误的代码打交道的程序员。

weixin_35794316的博客 342

Web渗透(一)CRLF注入简介

CRLF注入简介1、基础知识2、原理分析3、漏洞挖掘4、防御手段5、CRLF Payload: 1、基础知识 HTTP报文结构 从上图可以看到:HTTP报文由三部分组成:状态行、首部、主体。 可以看到状态行和首部中的每行都是以回车符(\r,%0d,CR)和换行符(\n,%0a,LF)结束。 十六进制编码分别为0x0d和0x0a,URL编码为%0D和%0A 这是因为HTTP规范中行应该使用CRLF结束。另外,首部和主体由两个CRLF分隔。 2、原理分析 CRLF是 “ 回车+换行 ”(\r\n)的简称。

weixin_39157582的博客 6818

【Cpp】C和C++混合编程

一、混合编程杂绪 1.1 为什么需要混合编程 Cpp是从C演变过来的,C有很多优秀成熟项目和库,没必要在C++中重写,C++程序可以直接调用调用 庞大项目划分后一部分适合用C(底层),一部分适合用C++(中间层、上层) 1.2 混合编程的支撑 编译型程序的编译过程:源文件->目标(库)文件->可执行程序->镜像文件 任何编程语言执行时都必须是可执行程序,所以都必须先被编译成...

baymaxly的博客 7731

TransUnet官方代码训练自己数据集(彩色RGB3通道图像的分割)

官方代码:https://github.com/Beckschen/TransUNet 目的:训练5个类别的汽车部件分割任务(测试在另一篇博客中) 实现效果: 1. github下载代码,并解压。 项目里的文件可能跟你下载的不一样,不急后面会讲到! 2. 配置数据集(尽最大努力还原官方数据集的格式)。 通常自己手上的数据集分images和labels文件夹,分别存放着原始图像和对应的mask图像,如下图所示; mask图像中的像素有0,1,2,3,4 分别代表背景,车身,轮子,车灯,窗户,一共五个

qq_37652891的博客 5万+

C++编程常见错误整理

C++编程常见错误整理                       最近,我在阅读《C++ Primer》,经常看到里面提到一些错误类型,像“编译错误”、“运行错误”等。于是,我对C++中的错误类型产生了兴趣,查阅了一些资料,特整理如下。        (一) 程序错误分类         在C++中,程序错误分为三类:语法错误(syntax error)、逻辑错误和运行错误。

lovelove5的专栏 1711

Leaflet 加载天地图完整配置指南:从插件修改到无限缩放(附源码)

本文详细介绍了Leaflet与天地图深度集成的完整配置指南,包括插件修改、无限缩放实现及源码示例。通过解析天地图服务的WMTS协议、双投影支持和密钥机制,帮助开发者突破缩放级别限制,优化地图应用性能,适用于Web地图开发和高精度GIS系统。

weixin_27180827的博客 440

Autosar MCAL-S32k324 Crypto配置-基于EB

NXP官网提供了免费的HSE固件,同时也提供了对应的协议栈。本文以算法为例,介绍MCAL相关的配置及在代码中的集成与使用。使用HSE计算,比软件算法确实方便许多。后面会完善更多的算法测试。

赞的博客 2434

C++中常见的编程错误及解决方法

C++是一门强大的语言,但它的复杂性也容易导致各种编程错误。通过了解并解决一些常见的编程错误,开发者可以提高代码的质量和稳定性。常见的错误包括语法错误、类型错误、内存管理错误、逻辑错误以及多线程错误。掌握这些错误的解决方法,能够帮助程序员更高效地进行调试,写出更稳定和高效的代码。通过实践这些技巧,程序员将能够减少编程中的错误,提高开发效率,编写出更加高质量的C++代码。

set_one的博客 1167

医院批量排班实战指南(三)日期列表生成与时段批量操作

在上一篇《批量排班功能实现指南(二):日期与时段设置》中,我们完成了日期范围选择和重复规则配置的界面搭建,实现了从第一步到第二步的跳转功能。现在我们已经可以选择日期范围和重复规则,但还缺少最关键的功能——根据规则生成日期列表并进行时段配置。本篇将继续深入批量排班的核心功能——日期列表生成与时段批量操作。根据日期范围和重复规则智能生成日期列表为每个日期配置上午、下午、夜诊时段批量时段操作提升配置效率日期列表的展示与管理。

低代码布道师的博客 1345

C++常见错误

C++的常见错误

programmerAlex的博客 771

嵌入式通信协议框架的四层架构设计与实现

本文详细介绍一种模块化的嵌入式通信协议框架设计,采用四层架构:硬件抽象层统一管理数据收发与缓冲,隔离硬件差异;协议管理层通过特征识别自动路由多协议数据,支持动态扩展;协议处理层独立实现各协议解析/构建,确保校验安全与零拷贝效率;应用层采用表驱动模式处理业务逻辑,与协议细节解耦。 该架构已应用于工业控制等领域,具备强类型安全、高扩展性和平台无关性特点,显著提升复通信场景下的开发效率与系统可靠性。提供完整代码实现与设计要点分析,助力开发者构建高效通信栈。

Bit_Dong的博客 2464

YOLO车辆测距、车辆识别与单目测距

YOLO(You Only Look Once)是一种实时物体检测系统,能够识别视频或图像中的多个对象,并确定其位置。结合车辆识别与单目测距技术,YOLO可以用于交通监控、自动驾驶等应用场景,通过单个摄像头实现车辆的识别与距离测量。YOLO结合单目测距技术,在多个应用领域展现出巨大的潜力与灵活性。其实时性和准确性为智能交通、自动驾驶等领域提供了强有力的技术支持。

走向CTO的路上... 1672

C++常见错误提示解释

1.编号:C2001 error C2001: newline in constant 直译:在常量中出现了换行。 错误分析: (1) 字符串常量、字符常量中是否有换行。 (2) 在这句语句中,某个字符串常量的尾部是否漏掉了双引号。 (3) 在这语句中,某个字符创常量中是否出现了双引号字符“"”,但是没有使用转义符“\"”。 (4) 在这句语句中,某个字符常量的尾部是否漏掉了单引号...

成长日记的博客 9357

药物不良反应预测实战指南:从FAERS数据挖掘到PyTorch深度学习模型(含可解释性分析)

【代码】药物不良反应预测实战指南:从FAERS数据挖掘到PyTorch深度学习模型(含可解释性分析)

理论都是虚的,代码才是王道。 721
上一篇: 华为面试
下一篇: JDBC介绍(一)
ybugchen
博客等级 码龄22年 1粉丝 44原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值