C++省略形参名
声明时,C和C++都可以省略形参名,但是通常不建议这样做。
在函数定义时,在C语言中,即使不使用形式参数,也不能省略参数名,而在C++中是可以的。
#include<stdio.h>
#include <stdlib.h>
void test(char);
int
main(void)
{
test('a');
}
void
test(char )
{
printf("hello\n");
}
编译如下:
lee@ubuntu:~/program/fun$ gcc test_parameter.c
test_parameter.c: In function ‘test’:
test_parameter.c:13: error: parameter name omitted
而在C++程序(修改以上代码至C++风格)中:
lee@ubuntu:~/program/fun$ mv test_parameter.c test_parameter.cpp
lee@ubuntu:~/program/fun$ gcc -lstdc++ test_parameter.cpp
lee@ubuntu:~/program/fun$ ./a.out
hello
另外:
gcc和g++都能编译C和C++程序,在gcc编译C++时,链接的库文件应该为libstdc++.so,而不是默认的libc.so,所有需要-lstdc++.so指明,否则会在链接时发生错误。所以编译C++文件时,得用如下命令:
gcc -lstdc++ -o a.out test.cpp