【问题标题】:I am learning C and can't figure out why this is giving me an error我正在学习 C,但无法弄清楚为什么这给了我一个错误
【发布时间】:2012-09-09 14:09:06
【问题描述】:

所以我正在尝试编写一个函数,它接收一个浮点值,返回一个浮点值,并用 printf 显示在 main 中。我不断收到相同的错误: markup2.c:58:错误:“GetPrice”的类型冲突 markup2.c:46: 错误:'GetPrice' 的先前隐式声明在这里

我在哪里犯了错误?我已经尝试过对涉及 GetPrice 的所有内容进行类型转换,但仍然没有运气。有人可以解释一下我做错了什么吗?

    #include<stdio.h>

// define the constant MARKUP as float value 0.1
#define MARKUP 0.1

int main()
{
    // declare variables
    char proceed;
    float value;
    float markupValue;

    // display welcome message to user and inform them of markup rate
    printf("\nThank you for using Chad Hammond's markup calculator.\n");
    printf("The current markup rate is %.1f%c.\n",MARKUP*100,'%');

    // ask user if they want to perform a markup
    printf("Would you like to markup an item? y/n:\n");
    scanf("%c", &proceed);

    // if yes, proceed to next step
    if(proceed == 'y')
    {
        // prompt user for the price of item to be marked up
        printf("Please enter a wholesale value to markup: ");
        scanf("%f", &value);

        // display input back to user
        printf("The wholesale value you entered was: %c%.2f\n",'$', value);
        markupValue = (value*MARKUP);

        // display amount of increase
        printf("The dollar amount of this increase will be: %c%.2f\n",'$', markupValue);

        // display total price after increse
        printf("The price for this item after markup is: %c%.2f\n",'$', (float)GetPrice(value));
    }else
        // if user did not want to markup an item, belittle them with sarcasm
        printf("Well, if you didn't want to markup an item you should have just used a Ti-83 and left me alone!\n\n");
    // display exit message
    printf("Thank you for using HammondInstruments Markup Calculator, goodbye.\n");

    // return that program has completed
    return 0;
}

float GetPrice(float value)
{
    float output;
    value += value*MARKUP;
    output = value;
    return (float)output;
}

【问题讨论】:

  • gcc -Wall -Wextra -o markup markup.c编译你会看到错误:)

标签: c function


【解决方案1】:

您需要在main 之前声明或定义GetPrice

【讨论】:

  • 谢谢你,成功了。我习惯了 Java,你可以在 main 之前或之后有一个方法,它运行良好。很高兴这是一个如此简单的错误,我一直在挠头好几个小时,试图看看我的函数中的逻辑是如何不正确的!谢谢大家!
  • 是的,这个是 C 语言特有的。既然你提到了 Java,我还要指出,与 Java 不同,在 C 语言的开头声明所有变量被认为是一种很好的做法您的功能(您已遵循)。另外,你能“接受”我的回答吗?
  • @DeepanjanMazumdar,不仅仅是好的做法。它曾经是必需的。
【解决方案2】:

C 期望在您使用它们之前看到它的函数(前向声明)。你在声明之前在main中使用了GetPrice(函数定义在main之后)。

当您的编译器看到GetPrice 的使用时,它会假定它是一个尚未看到的函数并生成一个隐式 声明,类似于int GetPrice()。稍后,当它看到函数声明时,它看到真正的函数签名与隐式声明不同,并抛出错误。

因此,解决方案是在main 之前定义GetPrice,或者在main 之前使用float GetPrice(float); 形式的前向声明。前向声明(就像您在 #include 的各种头文件中实际发现的一样)告诉编译器该函数存在,但将其定义留给以后(甚至是另一个文件)。

【讨论】:

  • 非常感谢您的详细解释,现在很有意义!
【解决方案3】:

你不应该先声明GetPrice 吗?然后main

【讨论】:

    【解决方案4】:

    您的主要问题是,由于您在main 之前没有声明一个,编译器会为您提供与您的不匹配的GetPrice 的默认声明,因此会发生冲突。您应该在main 之前添加一个原型,或者将整个函数移到那里:

    float GetPrice(float);
    //main
    float GetPrice(float value){...}
    

    float GetPrice(float value){...}
    //main
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-02-11
      • 1970-01-01
      • 1970-01-01
      • 2014-08-26
      • 2014-01-08
      • 1970-01-01
      相关资源
      最近更新 更多