2011-04-01 50 views
7

我一直在自學下了幾個月,當我有時間,我遇到了一個問題,我不知道如何解決。衝突的類型和x的先前的聲明在這裏......什麼?

具體而言,當我嘗試編譯這個使用gcc,我得到:

 
geometry.c:8: error: conflicting types for ‘trapezoid’ 
geometry.c:7: note: previous declaration of ‘trapezoid’ was here 
geometry.c:48: error: conflicting types for ‘trapezoid’ 
geometry.c:7: note: previous declaration of ‘trapezoid’ was here 
geometry.c:119: error: conflicting types for ‘trapezoid_area’ 
geometry.c:59: note: previous implicit declaration of ‘trapezoid_area’ was here 
geometry.c: In function ‘cone_volume’: 
geometry.c:128: error: called object ‘3.14100000000000001421085471520200371742248535156e+0’ is not a function 
geometry.c: In function ‘cylinder_volume’: 
geometry.c:136: error: called object ‘3.14100000000000001421085471520200371742248535156e+0’ is not a function 

現在,我想我可能需要強制轉換的功能,但同樣,我不知道。

它看起來要讀取PI,我已經定義爲3.141,爲函數。有沒有一種方法可以避免使用幻數3.141(儘管它比其他幻數小得多)?

//Geometric formulae 
#include <stdio.h> 
#include <math.h> 

#define PI 3.141 

float trapezoid(float b1, float b2, float h); 
int trapezoid(); 
float sphere_volume(float r); 
int sphere(); 
float cone_volume(float r, float h); 
int cone(); 
float pyramid_volume(float b, float h); 
int pyramid(); 
float cylinder_volume(float r, float h); 
int cylinder(); 

int main(void) { 
     char selection = 0; 

     while(selection != 'q') { 
       printf("\t--Geometric Formulas--\n"); 
       printf("\tSelection an option from the menu: "); 
       scanf("%c", & selection); 
       switch(selection) { 
         case 1: //Trapezoid area 
           printf("%d", trapezoid()); 
           break; 
         case 2: //Sphere volume 
           printf("%d", sphere()); 
           break; 
         case 3: //Cone volume 
           printf("%d", cone()); 
           break; 
         case 4: //Pyramid volume 
           printf("%d", pyramid()); 
           break; 
         case 5: //Cylinder volume 
           printf("%d", cylinder()); 
           break; 
         default: 
           break; 
       } 
     } 
} 
//  --Shape Menus-- 
//Trapezoid 
int trapezoid() { 
     float h = 0, b1 = 0, b2 = 0; 
     float traparea; 

     printf("\tTrapezoid base 1: "); 
     scanf("%3f", &b1); 
     printf("\tTrapezoid base 2: "); 
     scanf("%3f", &b2); 
     printf("\tTrapezoid height: "); 
     scanf("%3f", &b2); 

     traparea = trapezoid_area(b1, b2, h); 

     printf("\tTrapezoid Area: %3f\n", traparea); } 

//Sphere 
int sphere() { 
     float r = 0; 
     float spherevol; 

     printf("\tSphere radius: "); 
     scanf("%f", &r); 

     spherevol = sphere_volume(r); 

     printf("\tSphere volume: %3f\n", spherevol); } 

//Cone 
int cone() { 
     float r = 0, h = 0; 
     float conevol; 

     printf("\tCone radius: "); 
     scanf("%f", &r); 
     printf("\tCone height: "); 
     scanf("%f", &h); 

     conevol = cone_volume(r, h); 

     printf("\tCone volume: %3f\n", conevol); } 

//Pyramid 
int pyramid() { 
     float b = 0, h = 0; 
     float pyramidvol; 

     printf("\tPyramid base: "); 
     scanf("%f", &b); 
     printf("\tPyramid height: "); 
     scanf("%f", &h); 

     pyramidvol = pyramid_volume(b, h); 

     printf("\tPyramid volume: %3f\n", pyramidvol); } 

//Cylinder 
int cylinder() { 
     float r = 0, h = 0; 
     float cylindervol; 

     printf("\tCylinder radius: "); 
     scanf("%f", &r); 
     printf("\tCylinder height: "); 
     scanf("%f", &h); 

     cylindervol = cylinder_volume(r, h); 

     printf("Cylinder volume: %3f", cylindervol); } 

//  --Geometric Formulas-- 
//Trapezoid Area Computation 
float trapezoid_area(float b1, float b2, float h) { 
     return ((b1 + b2) * h)/2; } 

//Sphere Volume Computation 
float sphere_volume(float r) { 
     return ((pow(r,3)) * (4 * PI))/3; } 

//Cone Volume Computatioin 
float cone_volume(float r, float h) { 
     return ((PI(pow(r,2)) * h))/3; } 

//Pyramid Volume Computation 
float pyramid_volume(float b, float h) { 
     return (b * h)/3; } 

//Cylinder Volume Computation 
float cylinder_volume(float r, float h) { 
     return (PI(pow(r,2))) * h; } 

回答

6

任何他們那說有一個「隱」的定義:這可以通過聲明前就可以解決。例如,您預先聲明瞭浮動梯形int梯形,但您未預先聲明trapezoid_area。正如其他人指出的那樣,你也不能像在C++中那樣在C中重載。

在你試圖做隱含乘法幾個領域 - 例如,PI(POW(R,2))PI *(POW(R,2))

+2

我不介意提名,但是......我只回答了問題的一小部分。你確定你打算選擇我的正確答案嗎? – 2011-04-04 23:43:48

1

您定義了兩次函數梯形。 C不是C++。您不能像C++中使用重載功能一樣定義具有不同簽名的相同函數。

+0

謝謝... ...固定,但現在它顯示 /tmp/ccn6YvQ0.o:函數'sphere_volume': geometry.c :(.text + 0x39d):對'pow'的未定義引用 /tmp/ccn6YvQ0.o:在函數'cone_volume'中: geometry.c :(.text + 0x3d0):未定義的引用'pow' /tmp/ccn6YvQ0.o:函數'cylinder_volume': geometry.c :(.text + 0x41b):未定義引用'pow' collect2:ld返回1退出狀態 – Bowlslaw 2011-04-01 21:30:01

+0

@Bowlslaw:你還沒有鏈接數學庫。如果您使用的是linux,請使用-lm選項嘗試gcc。 – Heisenbug 2011-04-01 21:32:37

+0

儘管我已經鏈接了math.h庫,但-lm選項可行。 – Bowlslaw 2011-04-01 21:40:12

4

您所看到的錯誤是因爲你與不同的簽名

float trapezoid(float b1, float b2, float h); 
int trapezoid(); 

根據您的其他定義,它看起來像第一trapezoid功能應該被命名爲trapezoid_volume

float trapezoid_volume(float b1, float b2, float h); 
int trapezoid(); 
定義兩次同樣的功能
1
#define PI 3.141 

float trapezoid(float b1, float b2, float h); 
int trapezoid(); 

我想你想

float trapezoid_volume(...); 
/*    ^^^^^^^     */ 
3

Ç不支持函數重載。 C++一樣。

而且由於您不小心將trapezoid_volume標爲trapezoid,您會收到編譯器錯誤。

0

要回答你的問題有關定義PI - 圓周率通常在math.h定義(如M_PI),但因爲它不是一部分FO實際的標準,你可能需要做一些配置,以獲取它。

例如,使用MSVC時,您需要定義_USE_MATH_DEFINES才能得到它。對於GNU C庫的信息,請參閱

如果您的平臺沒有它,你可能要拋出類似以下內容的標題:

#ifndef 
#define M_PI 3.14159265358979323846264338327 
#endif 
相關問題