2012-04-18 73 views
0
#include<stdio.h> 
#include<math.h> 


int main(void){ 
    double a=0,r=0,n=0; 
    printf("Enter Constant a:"); 
    scanf("%lf",&a); 
    printf("Enter Constant r:"); 
    scanf("%lf",&r); 
    printf("Enter Variable n:"); 
    scanf("%lf",&n); 

    double an; 
    an = geom_rec(a,r,n); // Line 15 

    return 0; 
} 

double geom_rec(double a,double r,double n){ // Line 20 
    double ans=a; 
    return a; 
} 

錯誤:兩個雙打怎麼能是相互衝突的類型?

Line 20: error: conflicting types for 'geom_rec' 
Line 15: error: previous implicit declaration of 'geom_rec' was here 

回答

12

你忘了創建函數的原型。

main功能之前,將下面的(你也可以移動上述main整體功能):

double geom_rec(double a,double r,double n); 

如果調用沒有定義或之前原型的函數,編譯器假定它返回int - 這與您的實際返回類型衝突。

+5

+1值得說明默認'int'返回類型。 – hmjd 2012-04-18 15:14:26

+0

:P thx,即時通訊只是在我的課堂上學習,我有時會忘記這些事情。 THX如此! – user1082764 2012-04-18 15:15:32

+0

哎喲,那個答案的分數讓我意識到它實際上讓我的零代表增加:p – ThiefMaster 2012-04-18 15:17:46

4

您可以在main()之前放置該功能的原型,也可以在main()之前放置該功能本身。

0

當編譯器到達第15行時,它之前沒有看到函數geom_rec,所以它假定函數返回int

之後,在第20行中,將函數定義爲返回double並接受3 double參數,這與編譯器「知道」函數的不同有關。所以它抱怨說,讓你有機會在之前使用它來定義功能的正確原型。