在函数内部声明函数时出现语法错误
C++ Syntax error when declaring function inside function
我正在尝试创建一个程序,允许我找到一个数字的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
相关文章:
- C++使用 rand 定义函数语法
- QObject::连接不起作用 - 使用函数语法找不到信号
- C++ std::函数语法问题
- 如何在不更改现有函数语法的情况下将普通指针替换为共享指针
- 模板化函数语法错误中使用的模板化类中的嵌套类
- C 变化函数语法
- 返回语句中的构造函数语法
- 复制构造函数语法错误:myclass 没有成员 mymember
- C++.传递给函数.语法问题
- C++可视化工作室函数语法突出显示
- 在 c++ 中移动 2d 数组的构造函数(语法逻辑不清楚):
- c 通过值或指针函数语法
- 正确的朋友模板函数语法
- C++类构造函数语法的解释
- C++ 使用指针移动构造函数(*&&&语法)
- 复制构造函数语法并显示构造函数的值
- 回调函数语法
- 类模板专用化中的成员函数语法
- C++学习函数语法
- 使用模板 C++ 的友元函数语法错误