冲突的类型与X previous声明在这里......什么? [英] Conflicting types and previous declaration of x was here...what?
问题描述
我一直在自学下了几个月,当我有时间,我遇到了一个问题,我不知道如何解决。
具体而言,当我尝试编译这个使用gcc,我得到:
geometry.c:8:错误:冲突的类型梯形
geometry.c:7:注意:梯形previous声明在这里
geometry.c:48:错误:冲突的类型梯形
geometry.c:7:注意:梯形previous声明在这里
geometry.c:119:错误:冲突的类型'trapezoid_area
geometry.c:59:注意:previous的trapezoid_area'隐式声明在这里
geometry.c:在函数'cone_volume:
geometry.c:128:错误:调用的对象3.14100000000000001421085471520200371742248535156e + 0'不是一个函数
geometry.c:在函数'cylinder_volume:
geometry.c:136:错误:调用的对象3.14100000000000001421085471520200371742248535156e + 0'不是一个函数
现在,我想我可能需要类型转换的功能,但同样,我不知道。
看起来它想读PI,我已经定义为3.141,为函数。有没有一种方法可以让我避免使用幻数3.141(尽管它少得多一个神奇的数字比别人的)?
//几何公式
#包括LT&;&stdio.h中GT;
#包括LT&;&math.h中GT;#定义PI 3.141梯形浮动(浮动B1,B2浮球,浮球H);
INT梯形(上);
浮sphere_volume(浮点R);
INT球体();
浮cone_volume(浮点R,浮动H);
INT锥();
浮pyramid_volume(浮动B,浮动H);
INT金字塔();
浮cylinder_volume(浮点R,浮动H);
诠释气缸();诠释主要(无效){
字符选择= 0; 而(选择!='Q'){
的printf(\\ t - 几何公式 - \\ n);
的printf(\\ tSelection从菜单中选择一个选项:);
scanf函数(%C,&安培;选择);
开关(选择){
案例1://梯形面积
的printf(%d个,梯形());
打破;
案例2://球体体积
的printf(%d个球());
打破;
案例3://锥体体积
的printf(%d个圆锥());
打破;
壳4://金字塔体积
的printf(%d个金字塔());
打破;
案例5://汽缸容积
的printf(%D,气缸());
打破;
默认:
打破;
}
}
}
// --Shape Menus--
//梯形
INT梯形(){
浮H = 0,B1 = 0,B2 = 0;
浮traparea; 的printf(\\ tTrapezoid基地1:);
scanf函数(%3F,&安培; B1);
的printf(\\ tTrapezoid基地2);
scanf函数(%3F,&安培; B2);
的printf(\\ tTrapezoid高度:);
scanf函数(%3F,&安培; B2); traparea = trapezoid_area(B1,B2,H); 的printf(\\ tTrapezoid地区:%3F \\ n,traparea); }//领域
INT球体(){
浮R = 0;
浮spherevol; 的printf(\\ tSphere半径:);
scanf函数(%F,&安培; R); spherevol = sphere_volume(R); 的printf(\\ tSphere量:%3F \\ n,spherevol); }//锥体
INT锥(){
浮动使r = 0中,h = 0;
浮conevol; 的printf(\\ tCone半径:);
scanf函数(%F,&安培; R);
的printf(\\ tCone高度:);
scanf函数(%F,&安培; H); conevol = cone_volume(R,H); 的printf(\\ tCone量:%3F \\ n,conevol); }//金字塔
INT金字塔(){
浮B = 0中,h = 0;
浮pyramidvol; 的printf(\\ tPyramid基地:);
scanf函数(%F,和b);
的printf(\\ tPyramid高度:);
scanf函数(%F,&安培; H); pyramidvol = pyramid_volume(B,H); 的printf(\\ tPyramid量:%3F \\ n,pyramidvol); }//圆筒
诠释气缸(){
浮动使r = 0中,h = 0;
浮cylindervol; 的printf(\\ tCylinder半径:);
scanf函数(%F,&安培; R);
的printf(\\ tCylinder高度:);
scanf函数(%F,&安培; H); cylindervol = cylinder_volume(R,H); 的printf(气缸容积:%3F,cylindervol); }// --Geometric Formulas--
//梯形面积计算
浮trapezoid_area(浮点B1,B2浮球,浮球高){
返回((B1 + B2)* H)/ 2; }//球体体积计算
浮sphere_volume(浮点R){
返回((POW(R,3))*(4 * PI))/ 3; }//锥卷Computatioin
浮cone_volume(浮点R,浮高){
返回((PI(POW(R,2))* H))/ 3; }//金字塔量计算
浮pyramid_volume(浮动B,浮高){
回报(B×H)/ 3; }//汽缸容积计算
浮cylinder_volume(浮点R,浮高){
返回(PI(POW(R,2)))*小时}
任何人是说有一个'隐'的定义:这可以通过pre-宣布它来解决。举个例子,你pre-声明的浮动梯形的和的 INT梯形的,但你没有pre-申报的 trapezoid_area 的。正如其他人所指出的,也可以不是在C超载,你可以在C ++。
在你试图做隐含乘法几个领域 - 例如, PI(POW(R,2))的应该是的 PI *(POW(R,2)) 的
I've been teaching myself C for a few months when I have time, and I have run into a problem I am not sure how to fix.
Specifically, when I try to compile this using gcc, I get:
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
Now, I think I may need to typecast the functions, but again, I am not sure.
It looks like it wants to read PI, which I had defined as 3.141, as a function. Is there a way I can avoid using the magic number 3.141 (though it's much less of a magic number than others)?
//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; }
Any of them that say there was an 'implicit' definition: this can be solved by pre-declaring it. As an example, you pre-declared float trapezoid and int trapezoid, but you didn't pre-declare trapezoid_area. As others pointed out, you also can't overload in C as you can in C++.
In a few areas you're trying to do implicit multiplication -- eg, PI(pow(r,2)) should be PI * (pow(r,2)).
这篇关于冲突的类型与X previous声明在这里......什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!