C++链接错误 ld 返回 1 退出状态

发布于 2024-11-06 13:04:20 字数 4097 浏览 1 评论 0原文

我正在尝试编译,但我不断收到此错误,有人看到我的错误在哪里吗?

C:\Users\BRIAN'~1\AppData\Local\Temp\cc2Feaaa.o(.text+0x368) 在函数“main”中:

[链接器错误]未定义引用“secant(double, double, double, double, double) , double, double, double, double&, int&, double)

C:\Users\BRIAN'~1\AppData\Local\Temp\cc2Feaaa.o(.text+0x368) ld 返回 1 退出状态

using namespace std;
#include<iostream>
#include<cmath>
#include<iomanip>
#include<fstream>
// Declaration of functions used
void writetable (double, double, double, double);
void secant(double, double, double, double, double, double, double, double, double&, int&, double);
void writedata (double, double, double);
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;
x0= 1000;
x1 = 200;
for (double velocity = 16000; velocity <= 17500; velocity += 500)
{
        for (double 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, angle);

         }

}
  writetable(kr, uc, q, b);

 system("pause");
}

// 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 angle, 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;  

    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;

    if(root <1000) cout << "safe"<<endl<<endl; else cout <<"unsafe"<<endl<<endl;
    writedata(angle, velocity, root);
}
// Defines "fx" 
double fx(double radians,double velocity, double kr, double uc, double q, double b, double ts)
{
     return kr * pow(ts,4.0) + uc * ts - q - pow((velocity / b), 2.0) * sin(radians);


}
void writetable(double kr, double uc, double q, double b)
{
     cout <<endl << "Input Parameters:" <<endl;
     cout<< "Kr(1/K^2)=" << kr << endl << "uc(1/K)=" << uc <<endl << "q(unitless)=" << q << endl << "b(mph)=" << b<< endl; //not done this part yet
     cout << "  angle..............velocity...........surface temp..............safe..........";
     cout << "  degs...............mph................Kelvin.....................?............";
     cout << "--------------------------------------------------------------------------------";      

 }
 void writedata (double angle, double velocity, double root)
 {
  }

I am trying to compile but i keep getting this error, does anyone see where my mistake is?

C:\Users\BRIAN'~1\AppData\Local\Temp\cc2Feaaa.o(.text+0x368) In function `main':

[Linker error] undefined reference to `secant(double, double, double, double, double, double, double, double, double&, int&, double)

C:\Users\BRIAN'~1\AppData\Local\Temp\cc2Feaaa.o(.text+0x368) ld returned 1 exit status

using namespace std;
#include<iostream>
#include<cmath>
#include<iomanip>
#include<fstream>
// Declaration of functions used
void writetable (double, double, double, double);
void secant(double, double, double, double, double, double, double, double, double&, int&, double);
void writedata (double, double, double);
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;
x0= 1000;
x1 = 200;
for (double velocity = 16000; velocity <= 17500; velocity += 500)
{
        for (double 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, angle);

         }

}
  writetable(kr, uc, q, b);

 system("pause");
}

// 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 angle, 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;  

    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;

    if(root <1000) cout << "safe"<<endl<<endl; else cout <<"unsafe"<<endl<<endl;
    writedata(angle, velocity, root);
}
// Defines "fx" 
double fx(double radians,double velocity, double kr, double uc, double q, double b, double ts)
{
     return kr * pow(ts,4.0) + uc * ts - q - pow((velocity / b), 2.0) * sin(radians);


}
void writetable(double kr, double uc, double q, double b)
{
     cout <<endl << "Input Parameters:" <<endl;
     cout<< "Kr(1/K^2)=" << kr << endl << "uc(1/K)=" << uc <<endl << "q(unitless)=" << q << endl << "b(mph)=" << b<< endl; //not done this part yet
     cout << "  angle..............velocity...........surface temp..............safe..........";
     cout << "  degs...............mph................Kelvin.....................?............";
     cout << "--------------------------------------------------------------------------------";      

 }
 void writedata (double angle, double velocity, double root)
 {
  }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

余生共白头 2024-11-13 13:04:20

函数 secant 的声明

void secant(double, double, double, double, double, double, double, double, double&, int&, double);

与实际实现的签名不同:

void secant(double radians, double velocity, double kr, double uc, double q, double b, double x0, double x1, double angle, double& root, int& iteration)
{
    ... 
}

链接器尝试使用声明的参数列表查找实现,但没有找到。

The declaration of the function secant

void secant(double, double, double, double, double, double, double, double, double&, int&, double);

differs from the signature of the actual implementation:

void secant(double radians, double velocity, double kr, double uc, double q, double b, double x0, double x1, double angle, double& root, int& iteration)
{
    ... 
}

The linker is trying to find the implementation with the declared parameter list, but it doesn't find it.

停滞 2024-11-13 13:04:20

您提供的 secant() 实现包含错误的参数(9 个 double、一个 double 引用和一个 int< /code> 引用,而不是 8 个 double,即一个 double 引用和一个 int 引用)。

The implementation of secant() you have provided contains the wrong parameters (9 double's, a double reference and an int reference, rather than 8 doubles, a double reference and an int reference).

下雨或天晴 2024-11-13 13:04:20

您将割线声明为:

void secant(double, double, double, double, double, double, double, double, double&, int&, double);

但您将其定义为:

void secant(double , double , double , double , double , double , double , double , double , double& , int& )

You declare secant as:

void secant(double, double, double, double, double, double, double, double, double&, int&, double);

but you define it as:

void secant(double , double , double , double , double , double , double , double , double , double& , int& )
梦旅人picnic 2024-11-13 13:04:20

割线定义和声明似乎不同......
在声明中,最后一个参数是通过复制(double)传递的,在定义中是通过引用(int&)传递的。请看一下

secant definition and declaration seems to be different...
In declaration you have last argument pass-by-copy (double), in definition its by reference (int&). Please have a look

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文