在C和C ++中调用未声明的函数
在C中:
下面是C中的示例,
#include <stdio.h>
//没有提到参数列表
void func();
int main(){
//它在C中编译,但这不是
//我们应该做的事情
func(10);
return 0;
}
void func(int x)
{
printf("value: %d\n", x);
}输出:
value: 10
上面的一个成功编译。但是下面的代码不能在C中编译。
#include <stdio.h>
//没有提到参数列表
void func();
int main(){
//它不会编译
func('a');
return 0;
}
void func(char x)
{
printf("value: %c\n", x);
}输出:
main.c:14:6: error: conflicting types for ‘func’
void func(char x)
^~~~
main.c:15:1: note: an argument type that has a default promotion can’t match an empty parameter name list declaration
{
^
main.c:4:6: note: previous declaration of ‘func’ was here
void func();
^~~~在C++中:
下面是C++中的示例,
#include <bits/stdc++.h>
using namespace std;
//没有提到参数列表
void func();
int main(){
//它不会编译
func(10);
return 0;
}
void func(int x)
{
cout << "value: \n" << x << endl;
}输出:
main.cpp: In function ‘int main()’:
main.cpp:10:12: error: too many arguments to function ‘void func()’
func(10);
^
main.cpp:5:6: note: declared here
void func();
^~~~热门推荐
10 诗词送行祝福语大全简短
11 新房开工吉日祝福语简短
12 50多岁生日简短祝福语
13 安徽疫情祝福语简短英语
14 农民朋友发财祝福语简短
15 对生活祝福语简短精辟
16 搬家词简短祝福语朋友
17 女神结婚快乐祝福语简短
18 文学短句祝福语大全简短