pclint测试c或c实例Word下载.docx
《pclint测试c或c实例Word下载.docx》由会员分享,可在线阅读,更多相关《pclint测试c或c实例Word下载.docx(27页珍藏版)》请在冰点文库上搜索。
![pclint测试c或c实例Word下载.docx](https://file1.bingdoc.com/fileroot1/2023-5/9/fcae0cc2-013f-4073-99ee-4742747e3427/fcae0cc2-013f-4073-99ee-4742747e34271.gif)
(Info--newinconstructorforclass'
X'
whichhasnoassignmentoperator)
error1733:
whichhasnocopyconstructor)
{memset(p,20,'
test.cpp(9):
error669:
(Warning--Possibledataoverrunforfunction'
memset(void*,int,unsignedint)'
argument3(size=97)exceedsargument1(size=80)[Reference:
test.cpp:
lines7,9])
error831:
(Info--Referencecitedinpriormessage)
{deletep;
test.cpp(11):
error424:
(Warning--Inappropriatedeallocation(delete)for'
new[]'
data)
---Wrap-upforModule:
test.cpp
test.cpp
(2):
error753:
(Info--localclass'
(line2,filetest.cpp)notreferenced)
error900:
(Note--Successfulcompletion,8messagesproduced)
根据错误提示修改后的程序如下:
#include<
classX/*lint-e753*///只声明实现类X,没有写main()应用类x,故可以屏蔽。
{
int*p;
public:
X()//构造函数
{
p=NULL;
X(constX&
x)//拷贝构造函数
p=newint[20];
memcpy(p,x.p,20*sizeof(int));
X&
operator=(constX&
x)//赋值操作符
if(this==&
x)//检查自赋值
return*this;
int*temp=newint[20];
memcpy(temp,x.p,20*sizeof(int));
//复制指针指向内容
delete[]p;
//删除原有指针(将删除操作符放在后面,避免X=X特殊情况下,内容的丢失)
p=temp;
//建立新指向
voidinit()
{
if(NULL==p)return;
//判断指针是否为空
memset(p,'
20*sizeof(int));
~X()
};
//在};
后面回车换行以消除告警test.cpp(12):
注意:
为需要动态分配内存的类声明一个拷贝构造函数和一个赋值操作符(可参考effective_c++2e条款11)
再次运行pclint
PC-lintforC/C++(NT)Vers.9.00a,CopyrightGimpelSoftware1985-2008
---Module:
test.cpp(C++)
---GlobalWrap-up
error900:
(Note--Successfulcompletion,0messagesproduced)
实例2:
实现输入的两个复数的四则运算。
/****************************************
Filename:
Complex.h
Function:
Asimplecomplexcalculatordemo
****************************************/
iostream>
usingnamespacestd;
classcomplex
{
public:
//publicinterface
complex(doubler,doublei);
//constructor
voidassign(void);
//userinputtodefineacomplex
//overloadoperatorstofriendfunctions
friendcomplexoperator+(complex&
c1,complex&
c2);
friendcomplexoperator-(complex&
friendcomplexoperator*(complex&
friendcomplexoperator/(complex&
friendostream&
operator<
<
(ostream&
out,complexx);
private:
doublereal;
doubleimag;
Complex.cpp
#include"
Complex.h"
complex:
:
complex(doubler=0.0,doublei=0.0)
real=r;
imag=i;
return;
}
voidcomplex:
assign(void)
cout<
"
Pleaseinputtherealpart:
<
endl;
cin>
>
real;
Pleaseinputtheimaginarypart:
imag;
ostream&
out,complexx)
if(x.imag<
0)
return(out<
x.real<
+"
("
x.imag<
)"
i"
endl);
else
}
complexoperator+(complex&
c2)
returncomplex(c1.real+c2.real,c1.imag+c2.imag);
complexoperator-(complex&
returncomplex(c1.real-c2.real,c1.imag-c2.imag);
complexoperator*(complex&
returncomplex((c1.real*c2.real-c1.imag*c2.imag),(c1.real*c2.imag+c2.real*c1.imag));
complexoperator/(complex&
doubledenominator=c2.real*c2.real+c2.imag*c2.imag;
returncomplex((c1.real*c2.real+c1.imag*c2.imag)/denominator,(c1.imag*c2.real-c1.real*c2.imag)/denominator);
intmain(void)
complexc1(5,4),c2(2,10);
c1="
c1;
c2="
c2;
c1+c2="
c1+c2;
c1-c2="
c1-c2;
c1*c2="
c1*c2;
"
c1/c2="
c1/c2;
c1.assign();
Currentc1="
c2.assign();
Currentc2="
return0;
编译这两个文件,VC6.0编译器产生0errors0warnings,而pclint程序产生了如下27条警告信息,PC-lint告警信息如下:
Bcomplex.cpp(C++)
Bcomplex.h(19):
error1712:
(Info--defaultconstructornotdefinedforclass'
complex'
)
Bcomplex.cpp(30):
error1746:
(Info--parameter'
x'
infunction'
operator<
(std:
basic_ostream<
char,std:
char_traits<
char>
&
complex)'
couldbemadeconstreference)
Bcomplex.cpp(35):
error1764:
(Info--Referenceparameter'
c1'
(line32)couldbedeclaredconstref)
Bcomplex.cpp(32):
error830:
(Info--Locationcitedinpriormessage)
c2'
Bcomplex.cpp(40):
(line37)couldbedeclaredconstref)
Bcomplex.cpp(37):
Bcomplex.cpp(45):
(line42)couldbedeclaredconstref)
Bcomplex.cpp(42):
Bcomplex.cpp(51):
(line47)couldbedeclaredconstref)
Bcomplex.cpp(47):
Bcomplex.cpp(55):
error747:
(Info--Significantprototypecoercion(arg.no.1)inttodouble)
(Info--Significantprototypecoercion(arg.no.2)inttodouble)
error1754:
(Info--Expectedsymbol'
operator*='
tobedeclaredforclass'
operator+='
operator-='
operator/='
(Note--Successfulcompletion,27messagesproduced)
根据提示修改代码如下:
complex(void);
//voidconstructor
friendcomplexoperator+(constcomplex&
c1,constcomplex&
friendcomplexoperator-(constcomplex&
friendcomplexoperator*(constcomplex&
friendcomplexoperator/(constcomplex&
out,constcomplex&
x);
complex(void)
real=0;
imag=0;
x)
complexoperator+(constcomplex&
complexoperator-(constcomplex&
complexoperator*(constcomplex&
returncomplex((c1.real*c2.real-c1.imag*c2.imag),(c1.real*c2.imag+c2.real*c1.imag));
complexoperator/(constcomplex&
returncomplex((c1.real*c2.real+c1.imag*c2.imag)/denominator,(c1.imag*c2.real-c1.real*c2.imag)/denominator);
complexc1(5.0,4.0),c2(2.0,10.0);
c1*