冲突的类型和先前的 x 声明在这里......什么?

发布于 2024-10-30 03:34:07 字数 4554 浏览 1 评论 0原文

当我有时间的时候,我已经自学了几个月的 C 语言,但我遇到了一个我不知道如何解决的问题。

具体来说,当我尝试使用 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; }

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; }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

一场春暖 2024-11-06 03:34:07

他们中的任何人都说存在“隐式”定义:这可以通过预先声明来解决。例如,您预先声明了float trapezoidint trapezoid,但没有预先声明trapezoid_area。正如其他人指出的那样,您也不能像在 C++ 中那样在 C 中重载。

在某些领域,您尝试进行隐式乘法 - 例如,PI(pow(r,2)) 应该是 PI * (pow(r,2)) >。

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)).

橘亓 2024-11-06 03:34:07

您看到的错误是因为您使用不同的签名定义了同一函数两次

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

根据您的其他定义,第一个 trapezoid 函数应该命名为 trapezoid_volume

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

The errors you're seeing are because you're defining the same function twice with different signatures

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

Based on your other definitions it looks like the first trapezoid functions should be named trapezoid_volume

float trapezoid_volume(float b1, float b2, float h);
int trapezoid();
梦行七里 2024-11-06 03:34:07

C 不支持函数重载。 C++ 确实如此。

由于您不小心将 trapezoid_volume 声明为 trapezoid,因此您会收到编译器错误。

C does not support function overloading. C++ does.

And since you accidentally declared trapezoid_volume as trapezoid, you get a compiler error.

一杆小烟枪 2024-11-06 03:34:07

您定义了两次函数梯形。
C 不是 C++。您无法像在 C++ 中使用重载功能那样使用不同的签名定义相同的函数。

you defined twice function trapezoid.
C isn't C++. You can't define the same function with different signatures like you do in C++ using overloading features.

原谅我要高飞 2024-11-06 03:34:07
#define PI 3.141

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

我想你想要

float trapezoid_volume(...);
/*             ^^^^^^^                 */
#define PI 3.141

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

I think you want

float trapezoid_volume(...);
/*             ^^^^^^^                 */
谜兔 2024-11-06 03:34:07

要回答有关定义 PI 的问题 - pi 通常在 math.h 中定义(如 M_PI),但因为它不是实际的一部分标准,您可能需要进行一些配置才能获得它。

例如,当使用MSVC时,您需要定义_USE_MATH_DEFINES来获取它。请参阅

如果您的平台没有它,您可能需要将类似以下内容的内容放入标头中:

#ifndef
#define M_PI 3.14159265358979323846264338327
#endif

To answer your question about defining PI - pi is usually defined in math.h (as M_PI), but since it's not part fo the actual standard you might need to do some configuration to get it.

For example, when using MSVC you need to define _USE_MATH_DEFINES to get it. See

If your platform doesn't have it, you might want to throw something like the following into a header:

#ifndef
#define M_PI 3.14159265358979323846264338327
#endif
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文