我有一个非常简单的项目用于开始编程课程。我查看了涉及此错误的每个Stack Overflow问题,但没有一个问题可以解决我的问题,所以我决定问问自己。
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
答案 0 :(得分:1)
在lab5.cpp
中void perfomComputation(char c,double d){}
你错过了函数名中的字母'r'。这就是链接器无法找到头文件中声明的performComputation函数的实现的原因。
答案 1 :(得分:0)
我在void performComputation(char, double);
文件中看到.h
但在.cpp
中没有看到它我猜链接器也想知道它至少在你的代码中使用它的位置。< / p>
编辑: 是的,我刚在浏览器中使用了文本搜索:)
答案 2 :(得分:0)
lab5.cpp中的void perfomComputation(char c, double d)
拼写错误。