错误:调用对象类型';双倍';不是函数或函数指针 我对C++编程比较新,我有一个任务来编码牛顿-拉夫逊方法,但是我有错误错误: called object type 'double' is not a function or function pointer

错误:调用对象类型';双倍';不是函数或函数指针 我对C++编程比较新,我有一个任务来编码牛顿-拉夫逊方法,但是我有错误错误: called object type 'double' is not a function or function pointer,c++,pointers,function-pointers,C++,Pointers,Function Pointers,当我试图编译代码时出现此错误。我尝试了一些基本的改变来分配指针,但我可能用了错误的方式,我的代码打印在下面,有人能解释我如何克服这个问题吗 #include <iostream> #include <math.h> using namespace std; double f(double x); //this is f(x) double f(double x) { double eq1 = exp(x) + pow(x,3) + 5; return eq1;

当我试图编译代码时出现此错误。我尝试了一些基本的改变来分配指针,但我可能用了错误的方式,我的代码打印在下面,有人能解释我如何克服这个问题吗

#include <iostream>
#include <math.h>

using namespace std;

double f(double x); //this is f(x)
double f(double x) {
  double eq1 = exp(x) + pow(x,3) + 5;
  return eq1;
}

double f1(double x); //this is the first derivative f'(x)
double f1(double x) {
  double eq2 = exp(x) + 3*pow(x,2);
  return eq2;
}

int main() {
  double x, xn, f, f1, eps;
  cout << "Select first root :" << '\n'; //Here we select our first guess
  cin >> xn; 
  cout << "Select Epsilon accuracy :" << '\n';
  cin >> epsi;
  f = f(x);
  f1 = f1(x);
  cout << "x_n" << " " << "x_(n+1)"  << " " << "|x_(n+1) - x_1|" << '\n';
  do { 
    x = xn; //This is the first iteneration step where x takes the value of the last itenarated (known) root xn
    f = f(x);
    f1 = f1(x);
    xn = x - (f/f1); //this the formula that sets the itenaration going 
    cout << x << "     " << xn << "          " << fabs(xn - x) << '\n';
  }

  while( fabs(xn - x) < epsi ); //If |x_(n+1) - x_n| is smaller than the desired accurcay than the itenaration continues 
  cout << "The root of the equation is " << xn << '\n';

  return 0;
}
#包括
#包括
使用名称空间std;
双f(双x)//这是f(x)
双f(双x){
双eq1=exp(x)+pow(x,3)+5;
返回eq1;
}
双f1(双x)//这是一阶导数f'(x)
双f1(双x){
双eq2=exp(x)+3*pow(x,2);
返回eq2;
}
int main(){
双x,xn,f,f1,eps;
coutxn;
cout-epsi;
f=f(x);
f1=f1(x);

cout您正在尝试使用名为f和f1的函数以及名为f和f1的双精度函数。如果您调用变量或函数其他内容,则可以解决错误。最好的编码做法是为这些变量提供更好的名称,告诉读者它们做了什么,并避免像这样的错误。

您有局部变量与函数同名,因此

f = f(x);
f1 = f1(x);
不能工作

重命名函数或变量。无论如何,单字母变量/函数名称并不好。请使用描述性名称。您(或任何其他人)在几周后查看代码时会对此表示感谢

PS:您也不需要转发声明。而且函数可以写得更短一些:

//double f(double x); //  this you dont need
double f(double x) {
    return exp(x) + pow(x,3) + 5;
}
同样,在这种情况下,它几乎没有伤害,但你最好在它起作用之前戒掉这个坏习惯

最后但并非最不重要的一点是,您应该正确格式化代码

while( fabs(xn - x) < epsi ); 

因为通常当你看到一个while与一个
在同一行时,你会开始惊慌失措;)(while循环比while更常见,while循环中的条件出现后,由
引起的错误可能是调试过程中的一件痛苦事)

你的代码中有几个错误。我使它可编译:

#include <iostream>
#include <math.h>

using namespace std;

double func(double x); //this is f(x)
double func(double x) {
  double eq1 = exp(x) + pow(x,3) + 5;
  return eq1;
}

double func1(double x); //this is the first derivative f'(x)
double func1(double x) {
  double eq2 = exp(x) + 3*pow(x,2);
  return eq2;
}

int main() {
  double x, xn, f, f1, eps;
  cout << "Select first root :" << '\n'; //Here we select our first guess
  cin >> xn; 
  cout << "Select Epsilon accuracy :" << '\n';
  cin >> eps;
  f = func(x);
  f1 = func1(x);
  cout << "x_n" << " " << "x_(n+1)"  << " " << "|x_(n+1) - x_1|" << '\n';
  do { 
    x = xn; //This is the first iteneration step where x takes the value of the last itenarated (known) root xn
    f = func(x);
    f1 = func1(x);
    xn = x - (f/f1); //this the formula that sets the itenaration going 
    cout << x << "     " << xn << "          " << fabs(xn - x) << '\n';
  }

  while( fabs(xn - x) < eps ); //If |x_(n+1) - x_n| is smaller than the desired accurcay than the itenaration continues 
  cout << "The root of the equation is " << xn << '\n';

  return 0;
}
#包括
#包括
使用名称空间std;
double func(double x);//这是f(x)
双功能(双x){
双eq1=exp(x)+pow(x,3)+5;
返回eq1;
}
double func1(double x);//这是f'(x)的一阶导数
双功能1(双x){
双eq2=exp(x)+3*pow(x,2);
返回eq2;
}
int main(){
双x,xn,f,f1,eps;
coutxn;
cout-eps;
f=func(x);
f1=func1(x);

你能不能把它简化成一个复制错误的请。错误在哪一行?你说的指针在哪里?顺便说一句,它是“迭代”而不是“生成”如果在定义函数之前不使用它们,则无需对其进行前向声明。谢谢,是的,现在您已经解释清楚了,我仍然是新手,并且犯了很多错误。谢谢,这清除了所有问题,代码现在运行得很好:)
#include <iostream>
#include <math.h>

using namespace std;

double func(double x); //this is f(x)
double func(double x) {
  double eq1 = exp(x) + pow(x,3) + 5;
  return eq1;
}

double func1(double x); //this is the first derivative f'(x)
double func1(double x) {
  double eq2 = exp(x) + 3*pow(x,2);
  return eq2;
}

int main() {
  double x, xn, f, f1, eps;
  cout << "Select first root :" << '\n'; //Here we select our first guess
  cin >> xn; 
  cout << "Select Epsilon accuracy :" << '\n';
  cin >> eps;
  f = func(x);
  f1 = func1(x);
  cout << "x_n" << " " << "x_(n+1)"  << " " << "|x_(n+1) - x_1|" << '\n';
  do { 
    x = xn; //This is the first iteneration step where x takes the value of the last itenarated (known) root xn
    f = func(x);
    f1 = func1(x);
    xn = x - (f/f1); //this the formula that sets the itenaration going 
    cout << x << "     " << xn << "          " << fabs(xn - x) << '\n';
  }

  while( fabs(xn - x) < eps ); //If |x_(n+1) - x_n| is smaller than the desired accurcay than the itenaration continues 
  cout << "The root of the equation is " << xn << '\n';

  return 0;
}