我有一個非常簡單的項目開始編程類。我已經查看了涉及這個錯誤的每個Stack Overflow問題,並且他們都沒有幫助我解決問題,所以我決定問自己。C++ [鏈接器錯誤]未定義的引用'performComputation(char,double)'
lab5.cpp
#include <cstdlib>
#include <iostream>
#include <math.h>
#include "shape.h"
using namespace std;
int main()
{
char shape = '0';
char compType = '0';
double radius = 0.0;
double height = 0.0;
shape = inputShape();
while (shape != QUIT)
{
switch(shape)
{
case SPHERE:
inputDimension(radius);
compType = inputCompType();
performComputation(compType, radius);
break;
case CYLINDER:
case CONE:
inputDimension(radius, height);
compType = inputCompType();
performComputation(shape, compType, radius, height);
break;
}
}
cout << "GoodBye!";
system("PAUSE");
return 0;
}
char inputShape()
{
char c = '0';
cout << "Select a shape (1) Sphere, (2) Cylinder, (3) Cone (Q) Quit: ";
cin >> c;
return c;
}
void inputDimension(double& r)
{
cout << "Input a Radius: ";
cin >> r;
}
void inputDimension(double& r, double& h)
{
cout << "Input a Radius: ";
cin >> r;
cout << "Input a Height: ";
cin >> h;
}
char inputCompType()
{
char c = '0';
cout << "Select a Computation (1) Volume (2) Surface Area: ";
cin >> c;
return c;
}
void perfomComputation(char c, double d)
{
if(c == SURFACE_AREA)
{
cout << "The surface area of the Sphere is: " << sphere_surface_area(d);
}
else if (c == VOLUME)
{
cout << "the volume of the Sphere is: " << sphere_volume(d);
}
}
void performComputation(char b, char c, double d, double e)
{
if (b == CYLINDER)
{
if(c == SURFACE_AREA)
{
cout << "the surface area of the Cylinder is: " <<cylinder_surface_area(d,e);
}
else if (c == VOLUME)
{
cout << "the volume of the Cylinder is: " << cylinder_volume(d,e);
}
}
else if (b == CONE)
{
if (c == SURFACE_AREA)
{
cout << "the surface area of the Cone is: " << cone_surface_area(d,e);
}
else if (c == VOLUME)
{
cout << "the volume of the Cone is: " << cone_volume(d,e);
}
}
}
double sphere_surface_area(double r)
{
return (4.0) * PI * pow(r,2);
}
double sphere_volume(double r)
{
return (4.0/3.0) * PI * pow(r,3);
}
double cylinder_surface_area(double r, double h)
{
return ((2.0) * PI * pow(r,2))+((2.0) * PI * r * h);
}
double cylinder_volume(double r, double h)
{
return (PI * pow(r,2) * h);
}
double cone_surface_area(double r, double h)
{
return (PI*pow(r,2)) +((PI*r)*sqrt((pow(r,2) + pow(h,2))));
}
double cone_volume(double r, double h)
{
return (1.0/3.0)*(PI * pow(r,2) * h);
}
shape.h
#include <iomanip>
#include <cstdlib>
#include <iostream>
//constant declarations
const double PI = 3.141593;
const char SPHERE = '1';
const char CYLINDER = '2';
const char CONE = '3';
const char QUIT = 'Q';
const char VOLUME = '1';
const char SURFACE_AREA = '2';
//function declarations
char inputShape();
void inputDimension(double&);
void inputDimension(double&, double&);
char inputCompType();
void performComputation(char, char, double, double);
void performComputation(char, double);
double sphere_volume(double);
double sphere_surface_area(double);
double cylinder_volume(double, double);
double cylinder_surface_area(double, double);
double cone_volume(double, double);
double cone_surface_area(double, double);
我的錯誤是:
[Linker error] undefined reference to `performComputation(char, double)'
ld returned 1 exit status
C:\Users\AdminM\Documents\Cpp\Lab5\Makefile.win [Build Error] [Lab5.exe] Error 1
顯示確切的編譯命令。 「g ++」的參數順序很重要。 –