C中实际定义的函数的未定义参考错误 [英] Undefined reference error to function that is actually defined in C

查看:112
本文介绍了C中实际定义的函数的未定义参考错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写了这个程序来制作数字钻石。问题是,当我编译程序时,它将引发错误

I wrote this program to build a number diamond. The issue is that when I compile the program, it throws the error


build2.c :(。text + 0x5):未定义的引用`get_input'

build2.c:(.text+0x5): undefined reference to `get_input'

collect2:错误:ld返回1个退出状态

collect2: error: ld returned 1 exit status

我已经尝试了几个小时才能弄清楚问题出在哪里(例如,如果有拼写错误或类似的东西),但是函数调用看起来是相同的。我试图对其进行重命名,将其作为原型和实现来编写,但似乎没有任何效果。

I've tried for hours to figure out what exactly the problem is (e.g. if there is a spelling mistake or something similar), but the function call looks identical. I have attempted to rename it, write it as both a prototype and as an implementation, and nothing seems to work. Is there an issue that I'm not seeing?

//Define prior to main

int is_valid(int);
int get_input(void);
void print_pattern(int);

//Main
int main(void){
        int diamond_size;
        //diamond_size = get_input();

//value from get imput method used for diamond size

        print_pattern(get_input());

        return 0;
}

void print_pattern(int size){
int length, num, i, j;

//beginning of new diamond

printf("\n");

//Define each integer to work in layout of diamond
//First for loop fans out

for(i=1; i  <= size; i += 2){
        length = size-i+1;
        num =  1;
        printf("%*s", length," ");
        for(j = 0; j < i; j++){
                printf("%d ", num);
                num++;
        }
        printf("\n");
}

//second for loop fans in

for(i=size-2; i >= 1; i -= 2){
        length = size-i+1;
        num =   1;
        printf("%*s", length," ");
          for(j = 0; j < i; j++){
                printf("%d ", num);
                num++;
        }
        printf("\n");
}




int is_valid(int value){
int rem;

//uses remainder to determine if it is odd or even; an even number will not have a reaminder in this case

rem = value % 2;

if (rem == 0){
printf("You've entered a even number. Please try again.\n");
return (0);
}

//greater than 9 cnd
if (value > 9){
printf("You have entered a number greater than 9. Please try again.\n");
return (0);
}

//less than 1 cnd

if (value < 1){
printf("You have entered a number less than 1. Please try again.\n");
return (0);
}

return (1);

}

int get_input()
{
int cont, number, valid;
cont = 1;
while (cont = 1)
{
        printf("Enter an odd number less than 9 and greater than 0 < ");
        scanf("%d", &number);
        valid = is_valid(number);
        if (valid == 1)
        {
        cont = 0;
        }

}
return number;
}
}


推荐答案

您似乎具有嵌套功能;这是(a)非标准GCC扩展,和( b)我认为嵌套 get_input()函数的作用域是封闭函数,而不是文件作用域。解决方案是将 get_input()移到文件范围。在 print_pattern()的末尾添加一个额外的} ,然后删除最后一个} 放在文件末尾。

You seem to have nested functions; this is (a) a non-standard GCC extension, and (b) I presume the scope of the nested get_input() function is the enclosing function, not the file scope. The solution is to move get_input() to file scope. At the end of print_pattern() add an extra }, and delete the final } at the end of the file.

此外,请格式化您的代码-如今,大多数IDE都可以对其进行整理,并带有正确的缩进选项

Also, please format your code - most IDEs these days have options to tidy it up, and with correct indentation you may have seen your problem earlier.

哦,作为一项错误修复程序,您还可以在 get_input()

Oh, and as a bonus bug fix, you also have in get_input():

while (cont = 1)

这将永远是正确的-改用它:

This will always be true - use this instead:

while (cont == 1)

这篇关于C中实际定义的函数的未定义参考错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆