函数参数初始化,使用列表,C语言可以,C++不允许

同样的列表初始化,C语言不会报错

#include<stdio.h>
int sum(a,b,c)
int a;
int b;
int c;
{
    return a+b+c;
}
int main()
{
    printf("%d", sum(5,9,59));
}

正常运行

[Running] cd "d:\程序\随笔程序\2020年1月\" && gcc fun.c -o fun && "d:\程序\随笔程序\20201月\"fun
73
[Done] exited with code=0 in 6.014 seconds

但是C++会报错:

#include<iostream>
using namespace std;
int sum(a,b,c)
int a;
int b;
int c;
{
    return a+b+c;
}

int main()
{
    cout << sum(2,3,4);
}
[Running] cd "d:\程序\随笔程序\2020年1月\" && g++ canshuliebiao.cpp -o canshuliebiao && "d:\程序\随笔程序\20201月\"canshuliebiao
canshuliebiao.cpp:3:9: error: 'a' was not declared in this scope
 int sum(a,b,c)
         ^
canshuliebiao.cpp:3:11: error: 'b' was not declared in this scope
 int sum(a,b,c)
           ^
canshuliebiao.cpp:3:13: error: 'c' was not declared in this scope
 int sum(a,b,c)
             ^
canshuliebiao.cpp:3:14: error: expression list treated as compound expression in initializer [-fpermissive]
 int sum(a,b,c)
              ^
canshuliebiao.cpp:7:1: error: expected unqualified-id before '{' token
 {
 ^
canshuliebiao.cpp: In function 'int main()':
canshuliebiao.cpp:13:22: error: 'sum' cannot be used as a function
     cout << sum(2,3,4);
                      ^

[Done] exited with code=1 in 1.232 seconds

这是为什么呢?我现在还不理解

发布了267 篇原创文章 · 获赞 38 · 访问量 3万+

猜你喜欢

转载自blog.csdn.net/dghcs18/article/details/104076061