2014-02-05 106 views
0

我有一個非常簡單的項目開始編程類。我已經查看了涉及這個錯誤的每個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 
+0

顯示確切的編譯命令。 「g ++」的參數順序很重要。 –

回答

1

在lab5.cpp

無效perfomComputation(焦C,雙d){}

您錯過了函數名稱中的字母'r'。這就是爲什麼鏈接程序找不到頭文件中聲明的performComputation函數的實現。

+0

現在我感到絕對愚蠢,因爲我重寫了5次左右的這一行。 – PoniardDagger

0

我看到void performComputation(char, double);.h文件,但沒有在.cpp看到它我猜連接器也想知道它至少在你的代碼中使用它。

編輯: 在我的瀏覽器是的,我只是用文字搜索:)

0

在lab5.cpp的void perfomComputation(char c, double d)有拼寫 - R的字符缺失。