gpt4 book ai didi

c++ - 我面对 :Error 2 error C3861: 'fx' : identifier not found

转载 作者:行者123 更新时间:2023-11-28 07:04:11 26 4
gpt4 key购买 nike

我不太熟悉在 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;
}

我遇到了 2 个错误:错误 2 error C3861: 'fx': 找不到标识符错误 4 error C3861: '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++ - 我面对 :Error 2 error C3861: 'fx' : identifier not found,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21997793/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com