函数编译问题

[复制链接]
 楼主| txcy 发表于 2013-8-31 09:20 | 显示全部楼层 |阅读模式
为啥如下程序编译不通过
#include <iostream>
#include <cmath>
using namespace std;

int main()
{
int yy;
double x;
x=sqrt(9);
cout << "你好啊" <<f(2) << "\n";
return 0;
}
int f(int xxx)
{
return xxx*100;
}


改成下面形式,把函数f放在main前面就行了呢??
#include <iostream>
#include <cmath>
using namespace std;

int f(int xxx)
{
return xxx*100;
}

int main()
{
int yy;
double x;
x=sqrt(9);
cout << "你好啊" <<f(2) << "\n";
return 0;
}
无冕之王 发表于 2013-8-31 09:41 | 显示全部楼层
至少要main之前声明一下。
yybj 发表于 2013-8-31 09:43 | 显示全部楼层
#include <iostream>
#include <cmath>
using namespace std;

int f(int xxx); //这里加一个申明!

int main()
{
int yy;
double x;
x=sqrt(9);
cout << "你好啊" <<f(2) << "\n";
return 0;
}
int f(int xxx)
{
return xxx*100;
}
xsgy123 发表于 2013-8-31 09:58 | 显示全部楼层
你这个只需要在你需要使用这个函数之前的任何位置进行一下申明就没问题了,只用申明一次就行
您需要登录后才可以回帖 登录 | 注册

本版积分规则

274

主题

2106

帖子

0

粉丝
快速回复 在线客服 返回列表 返回顶部