功能问题,未定义的引用错误

时间:2017-10-25 01:52:41

标签: c++ function

我无法让我的功能在此程序中正常工作。我尝试编译时遇到以下错误:

/tmp/cc7oQapH.o: In function `main':polarcoord.cpp:(.text+0x7b): undefined reference to `degrees2radians(double)'
                                    polarcoord.cpp:(.text+0x99): undefined reference to `compute_coord(double, double, double, double)'
                                    collect2: ld returned 1 exit status

尽管在网上阅读了很多不同的内容,但我无法弄清楚如何解决这个问题。任何帮助将不胜感激!

#include <iostream>
#include <cmath>

using namespace std;

// FUNCTION PROTOTYPE FOR degrees2radians
double degrees2radians( double) ;   //variable for the function degrees2radians

// FUNCTION PROTOTYPE FOR compute_coord
void compute_coord( double, double, double, double) ;     //variable for the 
function compute_coord

// DO NOT MODIFY THE MAIN ROUTINE IN ANY WAY
int main()
{
  double angle_degrees(0.0), angle_radians(0.0), radius(0.0);
  double coord_x(0.0), coord_y(0.0);

  // Read in polar coordinates
  cout << "Enter radius: ";
  cin >> radius;

  cout << "Enter polar angle (degrees): ";
  cin >> angle_degrees;

  // Convert degrees to radians
  angle_radians = degrees2radians(angle_degrees);

  // Compute Cartesian (x,y) coordinates
  compute_coord(radius, angle_radians, coord_x, coord_y);

  // Output Cartesian coordinates
  cout << "Cartesian coordinates: ";
  cout << "(" << coord_x << "," << coord_y << ")" << endl;

  return 0;
}

// DEFINE FUNCTION degrees2radians here:
double degrees2radians( double angle_degrees, double & angle_radians)
{
  angle_radians = angle_degrees * (3.14 / 180);
  //return (angle_radians);
}


// DEFINE FUNCTION compute_coord here:
double compute_coord( double radius, double angle_radians, double & coord_x, 
double & coord_y)
{
  coord_x = radius * cos(angle_radians);
  coord_y = radius * sin(angle_radians);
  return (coord_x, coord_y);
}

1 个答案:

答案 0 :(得分:0)

#include <iostream>
#include <cmath>

using namespace std;

double degrees2radians( double) ; 
void compute_coord( double, double, double&, double&) ; 

int main()
{
  double angle_degrees(0.0), angle_radians(0.0), radius(0.0);
  double coord_x(0.0), coord_y(0.0);

  cout << "Enter radius: ";
  cin >> radius;

  cout << "Enter polar angle (degrees): ";
  cin >> angle_degrees;

  angle_radians = degrees2radians(angle_degrees);

  compute_coord(radius, angle_radians, coord_x, coord_y);

  cout << "Cartesian coordinates: ";
  cout << "(" << coord_x << "," << coord_y << ")" << endl;

  return 0;
}

double degrees2radians( double angle_degrees)
{
  return angle_degrees * (3.14 / 180);
}

void compute_coord( double radius, double angle_radians, double & coord_x, double & coord_y)
{
  coord_x = radius * cos(angle_radians);
  coord_y = radius * sin(angle_radians);
}

您的函数定义与声明

不匹配

也许这是正确的代码