【问题标题】:Error C2078: too many initializers错误 C2078:初始化程序过多
【发布时间】:2015-02-13 07:11:15
【问题描述】:

为什么这段代码不起作用?我的 IDE 是 Visual Studio 2013。

#include <stdio.h>
float tempatureGuide(float F, float C);
#define FREEZING_PT 32.0f
#define SCALE_FACTOR (5.0f/9.0f)
int main(void)
{
    float fahrenheit = 0.0;
    float celsius = 0.0 ;
    int convertTemp;
    printf ("Enter 0 to calculate Celsius or 1 to calculate Fahrenheit:            ");
    scanf ("%d", &convertTemp);

    if (convertTemp == 0)
    {
        // compute Celsius
        printf("Enter Fahrenheit temperture: ");
        scanf("%f", &fahrenheit);
        celsius = ((fahrenheit - FREEZING_PT) * SCALE_FACTOR);
        printf("Fahrenheit = %f  and Celsius = %f\n", fahrenheit, celsius);
        float tempatureGuide(fahrenheit, celsius);  // Error here
    }
    else
    {
        // compute fahrenheit
        printf("Enter the temperature in degrees fahrenheit\n\n");
        scanf("%f", &fahrenheit);
        celsius = (SCALE_FACTOR)* (fahrenheit - FREEZING_PT);
        printf("The converted temperature is %f", celsius);
        float tempatureGuide(fahrenheit, celsius);    // and here

    }
    return (0);
}

float tempatureGuide(float F, float C){
    if (F < 32 || C < 0)
        printf("It is freezing!");
    else if (F <= 60 || C <= 16)
        printf("It is cold");
    else if (F >= 70 || C >= 21)
        printf("It is just right");
    else if (F >= 82 || C >= 28)
        printf("It is warm");
    else if (F > 95 || C > 35)
        printf("It is hot");
    else
        printf("Please enter a number!");
    return (0);
}

这里的目标是添加到我之前所做的转换温度项目中,并向其中添加一个 if else 语句函数,该函数在 temp 上进行 cmet。我得到的错误是

Error   3   error C2078: too many initializes

在我调用函数的两条线上。我搜索了答案,但找不到任何答案。

【问题讨论】:

  • 修正缩进。而float tempatureGuide(fahrenheit, celsius); 并不是你调用函数的方式。
  • 我已从您的问题中删除了一些不相关的文字。我还缩进了一些代码,所以它显示为代码——但代码布局仍然急需改进。 一致缩进使代码更易于阅读。错误信息中有错字;您应该将确切的错误消息复制并粘贴到您的问题中。 C 和 C++ 是两种不同的语言;你用的是哪一个?您的代码看起来可能同样有效,但错误消息让我认为您正在使用 C++ 编译器编译它。如果您正在编写 C 语言,请将您的 IDE 配置为编译 C。
  • 请注意,100 C 或 212 F 的温度将被报告为“恰到好处”,因为您已从 &lt;&lt;= 切换到 &gt;=
  • 感谢您的反馈,以后我会尽量缩进。我使用的是 C 而不是 C++ 将我的编译器更改为 C。

标签: c++ c function compiler-errors


【解决方案1】:

这一行看起来像是尝试使用太多参数(因此出现“太多初始化器”错误)对 float 进行 C++ 初始化,而不是函数调用。

float tempatureGuide(fahrenheit, celsius);

大概你想调用函数并将结果存储在一个变量中:

float temp = tempatureGuide(fahrenheit, celsius);

或者只是调用函数而忽略返回值:

tempatureGuide(fahrenheit, celsius);

特别是因为你的函数总是返回0,所以有人可能会质疑是否需要非空返回类型。

【讨论】:

  • 哇,谢谢你,我知道我做错了什么,现在帮助很大。
【解决方案2】:

你需要调用一个函数

float tempatureGuide(float fahrenheit, float celsius) { //...}

作为

float retval = tempatureGuide(fahrenheit, celsius);

或最少

tempatureGuide(fahrenheit, celsius);  // not need to use return value

仅限。

【讨论】:

  • @juanchopanza 抱歉,复制粘贴太匆忙了!
  • @juanchopanza 现在应该没问题了。 :-)
【解决方案3】:

这只是一个简单的错误。请更改您调用 tempatureGuide(fahrenheit, celsius); 的两行代码;功能如下。

float tempatureGuide(fahrenheit, celsius); --> float ret = tempatureGuide(fahrenheit, celsius);

我在我的 VS2013 中测试了与您在上述更改中使用相同的方法。我能够成功编译并运行它。见附件。

【讨论】:

  • 这并没有真正为其他答案中已有的内容添加任何有用的信息。
  • 这是一个合理的答案,他的代码在上述更改下运行良好。!这就是为什么还要添加屏幕截图的原因。 !
  • 截图很少有用。您不必证明它有效;我们会相信你的话(如果没有,我们会抱怨)。
【解决方案4】:

只需调用该函数并将其返回给相同类型的变量即可。

【讨论】:

  • 这里有一个很好的例子。
猜你喜欢
  • 2016-06-09
  • 1970-01-01
  • 1970-01-01
  • 2014-12-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多