首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C++ >

请问自定义函数编译不通过的有关问题,多谢

2013-09-06 
请教自定义函数编译不通过的问题,谢谢为啥如下程序编译不通过#include iostream#include cmathusing n

请教自定义函数编译不通过的问题,谢谢
为啥如下程序编译不通过
#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;
}

[解决办法]
至少要mian之前声明一下。
[解决办法]
你这个只需要在你需要使用这个函数之前的任何位置进行一下申明就没问题了,只用申明一次就行
[解决办法]

引用:
多谢,是C++规定,凡是放在main函数后面的函数,必须要申明吗?
如果我有1000个函数呢,那申明不是要1000行了?

Quote: 引用:



#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;
}

写在一个头文件里,然后源文件包含头文件!不管多少函数,c++中都是要提前申明的

热点排行