C++ 我面临:错误2错误C3861:';fx';:找不到标识符

C++ 我面临:错误2错误C3861:';fx';:找不到标识符,c++,C++,我不太熟悉如何在cpp中定义函数,但像往常一样,我的定义如下: #include<iostream>; #include<Windows.h>; using namespace std; int main(){ int n; int power; //f(x) = x^power-n=0 cout << "please enter number of root: " << endl; cin >> power; cout <&

我不太熟悉如何在cpp中定义函数,但像往常一样,我的定义如下:

#include<iostream>;
#include<Windows.h>;
using namespace std;
int main(){

int n;
int power;

//f(x) = x^power-n=0
cout << "please enter number of root: " << endl;
cin >> power;
cout << "what number you want to get root: "<< endl;
cin >> n;
double x = 2;//it's the first x0 that newton algorithm needs

for (int i = 0; i<20000 ;i++)
{
   //f(x) = x^power-n=0 , f'(x) = 2*x //fx:x^power
   x = x - (fx(power,x) - n)/dif(power,x);
}
cout << "answer is " << x << endl;
return 0;

}

double fx (int power,int x){
for (int i = 1; i<power; i++)
{
    x = x*x;    
}
return x;
}

double dif (int power,int x){
//f(x) = x^power-n=0 -> f'(x) = power * x^(power-1)
if(power>1)
{
     for(int i = 1; i<power-1 ;i++){
     x = x*x;
     }
x = x*power;
return x;
}
return 1;
}
#包括;
#包括,;
使用名称空间std;
int main(){
int n;
整数幂;
//f(x)=x^power-n=0
功率;
cout n;
double x=2;//这是牛顿算法需要的第一个x0
对于(int i=0;i1)
{

对于(int i=1;i任何名称都应在使用前声明。您使用的是函数名
fx
,但您将其声明(同时也是其定义)放在其名称的使用之后。将其放在其声明之前

double fx (int power,int x);

这同样是有效的相对函数
dif

放置函数声明

double fx (int power,int x);
double dif (int power,int x);
在使用它们之前

using namespace std;

double fx (int power,int x);
double dif (int power,int x);

int main(){

现在你的函数对于你试图调用它们的地方来说是未知的。

你熟悉函数原型吗?在C++中,你需要先声明一些东西,然后才能使用它(定义仍然可以出现)。因此,要么在
main
之前添加
fx
dif
的原型,要么将它们移到
main
之前。实际上不是:]我两周前才开始cpp!但我知道用c或java定义函数的基础……是的,这就是重点:-]谢谢@crashmstr(Y)@关于C++的USER 3125076,这是它与C++之间的许多不同之处之一。