标签:
- /*正确*/
- #include <iostream>
- int f(int a=5);
- int f(int a)
- {
- std::cout <<a<<std::endl;
- return a;
- }
- int main()
- {
- f();
- return 0;
- }
- /*正确*/
- #include <iostream>
- int f(int a=5)
- {
- std::cout <<a<<std::endl;
- return a;
- }
- int main()
- {
- f();
- return 0;
- }
- /*正确*/
- #include <iostream>
- int f(int a);
- int f(int a=5)
- {
- std::cout <<a<<std::endl;
- return a;
- }
- int main()
- {
- f();
- return 0;
- }
- /*错误*/
- #include <iostream>
- int f(int a=5);
- int f(int a=5)
- {
- std::cout <<a<<std::endl;
- return a;
- }
- int main()
- {
- f();
- return 0;
- }
- [niuxinli@localhost ~]$ make test
- g++ test.cpp -o test
- test.cpp: In function ‘int f(int)’:
- test.cpp:3: error: default argument given for parameter 1 of ‘int f(int)’
- test.cpp:2: error: after previous specification in ‘int f(int)’
- make: *** [test] Error 1
- #include <iostream>
- int f(int a,int b);
- int f(int a=5,int b)
- {
- std::cout <<a<<std::endl;
- return a;
- }
- int main()
- {
- f(6);
- return 0;
- }
- g++ test.cpp -o test
- test.cpp: In function ‘int f(int, int)’:
- test.cpp:3: error: default argument missing for parameter 2 of ‘int f(int, int)’
- make: *** [test] Error 1
- [niuxinli@localhost ~]$ cat test.cpp
- #include <iostream>
- int x = 5;
- int f(int a,int b,int c);
- int f(int a,int b=5,int c=x)
- {
- std::cout <<a+b+c<<std::endl;
- return a;
- }
- int f2(int (*func)(int,int,int)=f )
- {
- func(2,3,5);
- return 0;
- }
- int main()
- {
- f(1);
- f2();
- return 0;
- }
- [niuxinli@localhost ~]$ make test && ./test
- g++ test.cpp -o test
- 11
- 10
- [niuxinli@localhost ~]$ cat test.cpp
- #include <iostream>
- int x = 5;
- int f(int a,int b,int c);
- int f(int a,int b=5,int c=x)
- {
- std::cout <<a+b+c<<std::endl;
- return a;
- }
- int f2(int (*func)(int,int,int)=f )
- {
- func(2);
- return 0;
- }
- int main()
- {
- f(1);
- f2();
- return 0;
- }
- [niuxinli@localhost ~]$ make test
- g++ test.cpp -o test
- test.cpp: In function ‘int f2(int (*)(int, int, int))’:
- test.cpp:11: error: too few arguments to function
- make: *** [test] Error 1
标签:
原文地址:http://www.cnblogs.com/duguochao/p/4489088.html