我该如何解决此错误:未定义引用`distance(float,float,float,float,')

时间:2018-11-03 01:01:10

标签: c++ undefined

所以我不太确定该怎么做,我已经多次返回代码,这一切似乎都是正确的,但是我一直在获取错误代码

Stubblefield9.cpp:74:未定义对`distance(float,float,float,float)'的引用 collect2.exe:错误:ld返回1退出状态

如果有人可以帮助我,这就是我的代码。

#include <iostream>
#include <iomanip>
#include <cmath>

using namespace std;

int main()
{
const float pi=3.14;
int choice;
char again;

   do
   {
       cout << "  IHCC Computer Science Registration Menu\n";
       cout << "  ======================================\n";
       cout << "  1.  The Volume of a Cone\n";
       cout << "  2.  The Volume of a Sphere\n";
       cout << "  3.  The Area of an Octagon\n";
       cout << "  4.  The Destance between two Points\n";
       cout << "  ======================================\n";
       cout << "  Enter your selection: ";

cin >> choice;

switch (choice)
{
case 1:
    float coneRadius,coneHeight,coneVolume;
    cout<<"Enter the Radius of the cone:";
    cin>>coneRadius;
    cout<<"\nEnther the Height of the Cone: ";
    cin>>coneHeight;
    coneVolume=pi*coneRadius*coneRadius*coneHeight/3;
    cout<<"\nThe Volume of a Cone with Radius ("<< coneRadius << ") and  Height (" << coneHeight << setprecision(2) << fixed << ") is " << coneVolume;
break;
case 2:
float sphereRadius,sphereVolume;
   cout << "Please insert the Radius: ";
   cin >>sphereRadius;
   sphereVolume = (4/3)*(pi)*(sphereRadius*sphereRadius*sphereRadius);
   cout<<"Volume with radius "<< setprecision(1) << fixed << sphereRadius << setprecision(2) << fixed << " is "<<sphereVolume;
break;
case 3:
float octagonSide, octagonArea;
cout << "Input side length: ";
 cin >> octagonSide;
octagonArea =(2 * (1 + sqrt(2)) * octagonSide * octagonSide); 
cout << "\nThe area of the octagon with side length (" << octagonSide << setprecision(2) << fixed << ") is "<< octagonArea;

break;
case 4:
float x, y, a, b, answer;

  float distance(float x, float y, float a, float b);
       cout << "Enter the points for the coordinates";
       cout << endl;
       cout << "Point x for first coordinates: ";
       cin >> x;
       cout << endl;
       cout << endl;
       cout << "Point y for first coordinate: ";
       cin >> y;
       cout << endl;
       cout << endl;
          cout << "Point x for the second coordinate: ";
       cin >> a;
       cout << endl;
       cout << endl;
       cout << "Point y for the second coordinate: ";
       cin >> b;
       cout << endl;
       cout << endl;
       answer = distance(x, y, a, b);
       cout << "The answer is " << answer;
break;
default:
cout << "Not a Valid Choice. \n";
cout << "Choose again.\n";
cin >> choice;
break ;
}
     cout << "\n\n Would you like to do it again(y or n)";
     cin >> again;
  }  while( again == 'y' || again == 'Y' );
return 0;
}

1 个答案:

答案 0 :(得分:1)

由于尝试调用代码中声明的功能distance(),您会收到此错误

  

float distance(float x, float y, float a, float b);

但未定义。