2016-09-24 54 views
0

我是比较新的C++编程,我有一个分配来编码牛顿拉夫逊法,但是我有错误的错误:错误:称为对象类型“双”不是一个函数或函数指针

called object type 'double' is not a function or function pointer 

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

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

谢谢

+2

你可以减少到一个[MCVE],请重现错误。 –

+1

其中哪行是错误?你在说什么指针?顺便说一句,它是“迭代”而不是“iteneration” – user463035818

+0

你不需要转发声明的功能,如果你不使用它们之前,他们被定义。 – user463035818

回答

0

你试图使用函数调用f和f1和双打称为F和F1。如果您调用其他变量或函数,则可以解决错误。给这些变量提供更好的名称,告诉读者他们做了什么,并避免像这样的错误,这将是一种良好的编码习惯。

+0

谢谢,是的,现在看来很清楚,你解释它,我仍然是新的,并且提供了大量的这些不准确之处。 – Procracker15

1

您有局部变量具有相同名称的功能,从而

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

using namespace std; is considered bad practice。在这种情况下,它没有什么坏处,但是在它确实重要之前,你最好摆脱这种坏习惯。

最后但并非最不重要的,你应该适当地格式化你的代码。这

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

看起来非常讨厌,因为它似乎是一个无限循环。我几乎从来不使用do-while循环,但是,我建议你把它写这样的:

do { 
    // ... 
} while(); 

因为通常当你在同一行中看到了一段与;你应该开始恐慌;)(虽然循环比do-while更常见,并且在while循环中的条件之后引起的错误可能是a **中的一个痛苦调试)

+0

谢谢,这清除了一切,代码现在完美:) – Procracker15

0

代码中有几个错误。我把它编译:

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

的主要问题是:

  1. 你与f(x)功能的相同名称定义的变量f(重复了f'(x)功能相同的错误)和
  2. 您在程序中声明eps变量代表epsilon,但您试图通过调用epsi多次访问该变量。
相关问题