繁体   English   中英

重新定义; 先前的定义是“数据变量”

[英]Redefinition; previous definition was 'data variable'

好吧,我要失去理智了。 我应该为课堂编写一个程序,该程序确定您输入的年份(代表day年)的天数,我感觉自己不知道自己在做什么。 在压力水平进一步提高之前,我将获得任何帮助,我将不胜感激。 我什至在无法测试我的程序之前遇到的最终错误(我怀疑这是行得通的)是:

error C2365: 'leap' : redefinition; previous definition was 'data variable'

error C2365: 'count' : redefinition; previous definition was 'data variable'

源代码:

#include <stdio.h>

int main(void){

    int d, m, y, x, day, leap, count;


    //Input values for date
    printf("\n Input the Day: ");
    scanf_s("%f", &d);

    printf("\n Input the Month: ");
    scanf_s("%f", &m);

    printf("\n Input the Year: ");
    scanf_s("%f", &y);
    //End input

    int count(int m, int day);

    int leap(int y, int x);



    if (x == 1){
            printf("\nThe day of the year is %f", day + 1);
    }
    else{
            printf("\nThe day of the year is %f", day);
    }

    system("pause");
    return(0);
}


//Count the number of days
int count(int m, int day, int d){
    if (m == 1){
        day = d;
    }
    if (m == 2){
        day = 31 + d;
    }
    if (m == 3){
        day = 59 + d;
    }
    if (m == 4){
        day = 90 + d;
    }
    if (m == 5){
        day = 120 + d;
    }
    if (m == 6){
        day = 151 + d;
    }
    if (m == 7){
        day = 181 + d;
    }
    if (m == 8){
        day = 212 + d;
    }
    if (m == 9){
        day = 243 + d;
    }
    if (m == 10){
        day = 273 + d;
    }
    if (m == 11){
        day = 304 + d;
    }
    if (m == 12){
        day = 334 + d;
    }
    return(day);
}

//Determine if it's a leap year
int leap(int y, int x){
    if (y % 400 == 0){
        x = 1;
    }
    else if (y % 100 == 0){
        x = 0;
    }
    else if (y % 4 == 0){
        x = 1;
    }
    else{
        x = 0;
    }
    return(x);
}

无需声明您的函数像数据类型一样leap count

int d, m, y, x, day, leap, count;

删除leapcount如下

int d, m, y, x, day;

下面是您的函数原型,并且是正确的(对于编译器来说足够了)

int count(int m, int day);

int leap(int y, int x);

countleapmain变量中相同的变量和函数名称,无效。

以下是函数声明,它们在main()是不允许的(局部函数定义是非法的)。 您应该将它们放在main()之前。

int count(int m, int day);
int leap(int y, int x);

另外,根据您的定义, count()的函数声明应为:

int count(int m, int day, int d);

并删除int leap, count; ,这是针对数据类型(此处为int )的声明,而不是针对函数的声明。

如果要在main()调用它们,只需使用:

count(m, day, d);
leap(y, x);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM