C++ 编程常见错误收集

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;

}

明天继续.....

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值