在函数内部声明函数时出现语法错误

C++ Syntax error when declaring function inside function

本文关键字:函数 语法 错误 内部 声明      更新时间:2023-10-16

我正在尝试创建一个程序,允许我找到一个数字的n次方根。我试着在xcode上运行这个,我得到一个错误,阻止我运行它。这一行出现错误:

double f(double x) {

在这一行中,我试图声明一个函数,但似乎我声明不正确。xcode说是expected ;。如何解决这个问题?

#include <iostream>
#include <string>
#include <cmath>
#include <iomanip>
#include <cctype>
#include <stdio.h>
#include <ctype.h>
#include "helloworld.h"
#include <locale>
using namespace std;
double newton( int n, int number);
string lowercase (string word);
int main()
{
    int n;
    int number;
    cout << "This program can find the nth root of a number without actually solving the problem" <<endl;
    cout << "Tell me what the value of n is: ";
    cin >> n;
    cout << "What is the number that you want to get rooted";
    cin >> number;
    newton(n, number); 
}
double newton( int n, int number) {
    const double epsilon = .0001;
    double x0;
    double x1 = number;
    double f(double x) {
        return (double) pow(x, n) - number;
    }
    double der_f(double x) {
        return (double) n*pow(x, n-1);
    }
    while( abs(x1-x0) < epsilon) {
        x0 = x1;
        x1 = x0 -f(x0)/der_f(x0); 
    }
    return x1;
}

如果你真的想要函数中的函数-有一个hack。可以在函数内部定义带有静态函数的struct。

的例子:

double newton( int n, int number) {
    const double epsilon = .0001;
    double x0;
    double x1 = number;
    struct wrap {
       static int n;
       static double f(double x) {
           return (double) pow(x, n) - number;
       }
       static double der_f(double x) {
           return (double) n*pow(x, n-1);
       }
    };
    wrap::n = n;
    while( abs(x1-x0) < epsilon) {
        x0 = x1;
        x1 = x0 -wrap::f(x0)/wrap::der_f(x0); 
    }
    return x1;
}

就像这样

Move

   double f(double x) {
        return (double) pow(x, n) - number;
    }
    double der_f(double x) {
        return (double) n*pow(x, n-1);
    }

to outside of double newton( int n, int number) {

发生这种情况是因为您在函数内部声明了新函数,这是不可能的。试试这个:

#include <iostream>
#include <string>
#include <cmath>
#include <iomanip>
#include <cctype>
#include <stdio.h>
#include <ctype.h>
#include "helloworld.h"
#include <locale>
using namespace std;
double newton( int n, int number);
string lowercase (string word);

double f(double x) {
    return (double) pow(x, n) - number;
}
double der_f(double x) {
    return (double) n*pow(x, n-1);
}
int main() {
  int n;
  int number;
  cout << "This program can find the nth root of a number without actually solving the problem" <<endl;
  cout << "Tell me what the value of n is: ";
  cin >> n;
  cout << "What is the number that you want to get rooted";
  cin >> number;
  newton(n, number);
}
double newton( int n, int number) {
  const double epsilon = .0001;
  double x0;
  double x1 = number;
  // and here call the functions
  f();
  der_f();
  while( abs(x1-x0) < epsilon ) {
    x0 = x1;
    x1 = x0 -f(x0)/der_f(x0); 
  }
  return x1;
}

正如已经指出的那样,您必须摆脱局部函数。c++ 11中的另一种方法是使用lambda-functions:

auto f = [=](double x) {
  return (double) pow(x, n) - number;
};
auto der_f = [=](double x) {
  return (double) n*pow(x, n-1);
};

在这种情况下,您所需要的只是将double func_name(double x)替换为auto func_name = [=](double x)

[=]是一个 λ -引入器。通过=,您告诉您希望所有的局部变量都可以在lambda函数中通过值访问。这是在你的情况下最合适的方法,因为你只有基本类型的变量,你不想从你的局部函数修改它们。

auto关键字告诉编译器从初始化子句中自动推断存储函数的变量类型。这里可以用std::function<double(double)>代替auto