为什么 X 有效但未声明?C++
why x works but is undeclared? c++
我基于割根查找的程序的示例问题之一使用 x,但没有声明它,但运行成功。同时,当我使用几乎相同的设置时,我收到一个错误,说 x 未声明。为什么它在示例程序中有效而不是我的?
using namespace std;
#include<iostream>
#include<cmath>
#include<iomanip>
// Declaration of functions used
void secant(double, double, double, double,
double, double, double&, int& );
double fx(double, double, double, double, double);
const double tol=0.0001; // Tolerance for convergence
const int max_iter=50; // Maximum iterations allowed
// main program
int main()
{
int iteration; // Number of iterations
double a, b, c, d; // Constants in f(x)
double x0, x1; // Starting values for x
double root; // Root found by secant method
cout<<"Enter a, b, c, and d"<<endl<<"separated by a space ";
cin>>a>>b>>c>>d;
cout<<"nEnter two initial values for x,nseparated by a space: ";
cin>>x0>>x1;
secant(a, b, c, d, x0, x1, root, iteration);// Call "secant"
// Output
cout<<"nThe root is = "<<root<<endl;
cout<<"The number of iterations was = "<<iteration<<endl;
cout<<"The value of f(x) at the root = "<<fx(a,b,c,d,root)<<endl<<endl;
system("pause");
return 0;
}
// Definition of function "secant"
// Receives a, b, c, d and x0 values from main program
// Returns root and the iterations required
void secant(double a,double b, double c, double d,
double x0, double x1, double& root, int& iteration)
{
double xnminus1, xnplus1, xn; // Local variables
iteration=0; // Initialize iterations
xnminus1=x0;
xn=x1;
do
{
++iteration;
xnplus1 = xn - fx(a,b,c,d,xn)*(xn-xnminus1)/
(fx(a,b,c,d,xn)-fx(a,b,c,d,xnminus1));
cout<<"x"<<iteration+1<<" = "<<xnplus1<<endl;
xnminus1 = xn;
xn=xnplus1;
}
while ((fabs(fx(a,b,c,d,xnplus1)) >= tol )&& (iteration < max_iter));
root=xnplus1;
}
// Defines "fx" -- calculates f(x) = ax^3+bx^2+cx+d
double fx(double a, double b, double c, double d, double x)
{
return a*pow(x,3)+b*pow(x,2)+c*x+d;
}
这是我的程序,我使用相同的结构,但它说 X 未声明?
using namespace std;
#include<iostream>
#include<cmath>
#include<iomanip>
#include<fstream>
// Declaration of functions used
void secant(double, double, double, double,
double, double, double, double, double&, int& );
double fx( double, double, double, double, double, double, double);
const double tol=0.0001; // Tolerance for convergence
const int max_iter=50; // Maximum iterations allowed
// main program
int main()
{
int iteration; // Number of iterations
double kr, uc, q, b, radians;
double x0, x1; // Starting values for x
double root; // Root found by secant method
const double PI = 4.0*atan(1.0);
ifstream datain ("shuttle.txt");
ofstream dataout ("results.txt");
datain >> kr >> uc >> q >> b;
int velocity = 16000;
double angle =10;
x0= 1000;
x1 = 200;
for (int velocity = 16000; velocity <= 17500; velocity += 500) {
for (int angle = 10; angle <= 70; angle += 15) {
radians= angle * PI/180 ;
cout << velocity << endl;
cout << radians << endl;
cout << angle << endl;
secant (radians, velocity, kr, uc, q, b, x0, x1, root, iteration);
// Output
cout<<"nThe root is = "<<root<<endl;
cout<<"The number of iterations was = "<<iteration<<endl;
cout<<"The value of f(x) at the root = "<<fx(radians, velocity, kr, uc, q, b, root)<<endl<<endl;
system("pause");
return 0;
}
// Definition of function "secant"
// Receives a, b, c, d and x0 values from main program
// Returns root and the iterations required
void secant(double radians,double velocity, double kr, double uc, double q, double b, double x0, double x1, double& root, int& iteration);
{
double xnminus1, xnplus1, xn; // Local variables
iteration=0; // Initialize iterations
xnminus1=x0;
xn=x1;
do
{
++iteration;
xnplus1 = xn - fx(radians, velocity, kr, uc, q, b, xn)*(xn-xnminus1)/
(fx(radians, velocity, kr, uc, q, b, xn)-fx(radians, velocity, kr, uc, q, b, xnminus1));
cout<<"x"<<iteration+1<<" = "<<xnplus1<<endl;
xnminus1 = xn;
xn=xnplus1;
}
while ((fabs(fx(radians, velocity, kr, uc, q, b, xnplus1)) >= tol )&& (iteration < max_iter));
root=xnplus1;
}
// Defines "fx" -- calculates f(x) = ax^3+bx^2+cx+d
double fx(double radians,double velocity, double kr, double uc, double q, double b, double x);
{
return kr * pow(x,4.0) + uc * x - q - pow(velocity/b, 2.0) * sin(radians);
}}
}
double fx(double radians,double velocity, double kr, double uc, double q, double b, double x);
该行末尾有一个额外的分号 ";"。 :)
应该是
double fx(double radians,double velocity, double kr, double uc, double q, double b, double x)
定义割线函数时也会遇到相同的错误。定义函数时,不要在末尾加上分号。
最终代码应如下所示
#include<iostream>
#include<cmath>
#include<iomanip>
#include<fstream>
// Declaration of functions used
void secant(double, double, double, double, double, double, double, double, double&, int& );
double fx( double, double, double, double, double, double, double);
const double tol=0.0001; // Tolerance for convergence
const int max_iter=50; // Maximum iterations allowed
using namespace std; // <- Goes after including the headers
// main program
int main()
{
int iteration; // Number of iterations
double kr, uc, q, b, radians;
double x0, x1; // Starting values for x
double root; // Root found by secant method
const double PI = 4.0*atan(1.0);
ifstream datain ("shuttle.txt");
ofstream dataout ("results.txt");
datain >> kr >> uc >> q >> b;
int velocity = 16000;
double angle =10;
x0= 1000;
x1 = 200;
for (int velocity = 16000; velocity <= 17500; velocity += 500)
{
for (int angle = 10; angle <= 70; angle += 15)
{
radians= angle * PI/180 ;
cout << velocity << endl;
cout << radians << endl;
cout << angle << endl;
secant (radians, velocity, kr, uc, q, b, x0, x1, root, iteration);
}
}
// Output
cout<<"nThe root is = "<<root<<endl;
cout<<"The number of iterations was = "<<iteration<<endl;
cout<<"The value of f(x) at the root = "<<fx(radians, velocity, kr, uc, q, b, root)<<endl<<endl;
system("pause");
return 0;
}
// Definition of function "secant"
// Receives a, b, c, d and x0 values from main program
// Returns root and the iterations required
void secant(double radians,double velocity, double kr, double uc, double q, double b, double x0, double x1, double& root, int& iteration)
{
double xnminus1, xnplus1, xn; // Local variables
iteration=0; // Initialize iterations
xnminus1=x0;
xn=x1;
do
{
++iteration;
xnplus1 = xn - fx(radians, velocity, kr, uc, q, b, xn)*(xn-xnminus1)/
(fx(radians, velocity, kr, uc, q, b, xn)-fx(radians, velocity, kr, uc, q, b, xnminus1));
cout<<"x"<<iteration+1<<" = "<<xnplus1<<endl;
xnminus1 = xn;
xn=xnplus1;
}
while ((fabs(fx(radians, velocity, kr, uc, q, b, xnplus1)) >= tol )&& (iteration < max_iter));
root=xnplus1;
}
// Defines "fx" -- calculates f(x) = ax^3+bx^2+cx+d
double fx(double radians,double velocity, double kr, double uc, double q, double b, double x)
{
return kr * pow(x,4.0) + uc * x - q - pow(velocity/b, 2.0) * sin(radians);
}
我假设你的意思是函数fx
中的x
变量:
x
被声明为参数,因此可以在没有显式局部变量声明的情况下使用它。 可能会让你失望的是 fx
的正向声明 没有为其参数命名
相关文章:
- #ifdef和未声明的标识符
- 未声明的标识符编译暗黑破坏神 2 程序"muleview"
- 在顶点着色器中使用 OpenGl 的未声明标识符,我在顶点着色器中绘制三角形时遇到问题
- Google protobuf 时间戳未声明标识符,在 Windows 上具有C++
- 在气泡排序程序中未声明错误功能
- 我在范围内未声明的错误类有问题
- 使用未声明的标识符"sqrt"?
- 我包含了<winuser.h>,但编译器在MonitorFromWindow上抛出"未声明"
- 对类 (C++) 中的私有变量使用未声明的标识符
- 我的代码中有错误,未声明的标识符
- 未定义的标识符/未声明
- 错误:在 C++ 'std'使用未声明的标识符
- 在编译器之前检测未声明的标识符
- 使用未声明的标识符'temp'
- 使用未声明的标识符"yylex"和"yyin"?
- 使用"class"关键字,后跟未声明的标识符
- 未声明的标识符错误,但变量定义正确 (?)
- C2065 'cout':未声明的标识符
- 在此范围内未声明错误"Clreol"
- C++模板模板参数中未声明的标识符