簡體   English   中英

我的未定義引用已定義

[英]my undefined reference is defined

當我嘗試編譯它時,它說“testrand.c :(。text + 0x11):未定義引用`rand_number'”

1 #include <stdio.h>
2 #include <time.h>
3 
4 int rand_number(int param);
5 
6 main()
7 {
8 while(5)
9 {
10         printf("%d", rand_number(15));
11         sleep(1);
12 }       
13 
14 
15 int rand_number(int param)
16  {
17   srand((unsigned int)time(NULL));
18     int x = param;
19     int rn = rand() % x;
20         return rn;
21 }       
22 }

但是,我已經清楚地在那里定義了......

我已經嘗試在引號中包含time.h,包括stdlib.h等...但仍然不知道發生了什么。 有誰知道發生了什么?

發生這種情況是因為你的rand_number函數是在其他函數main定義的。

這應該可以解決您的問題:

#include <stdio.h>
#include <time.h>

int rand_number(int param);

main()
{
    while(5)
    {
        printf("%d", rand_number(15));
        sleep(1);
    }
}

int rand_number(int param)
{
    srand((unsigned int)time(NULL));
    int x = param;
    int rn = rand() % x;
    return rn;
}

你已經在main定義了函數rand_number ,這是不允許的。

通過在第13行放置}來關閉main()。並從第22行中刪除}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM