簡體   English   中英

錯誤:“…”的類型沖突; 注意:之前的隱式聲明“…”在這里

[英]error: conflicting types for '…'; note: previous implicit declaration of '…' was here

我創建了一個文件。 C“ Sorting.c”實現幾種用於對整數數組進行排序的算法。 現在,我必須創建一個測試文件,該文件創建隨機數組並在這些數組上隨機執行各種排序算法。 此外,所產生的時間必須寫在終端和文本文件上。

我寫了這段代碼:

#include <stdio.h>
#include <time.h>
#include <stdlib.h>
#include <sys/time.h>
#include "Sorting.h" //file thath contains the implementation of the sorting method like iinsertion sort, selection sort, merge sort and quick sort 

#define N 100
#define STEP 5

int arrayOriginal[N];
int arrayToSort[N];
int arrayTemp[N];

void fillArray(int a[], int n, int max) {
    srand(time(NULL));
    int i;
    for(i = 0; i < n; i++) 
        a[i] = rand() % max;
}

void copyInto(int a[], int b[], int n) {
    int i;
    for(i = 0; i < n; i++)
        b[i] = a[i];
}

void testReport() {
    FILE* pFile = fopen("Times.txt", "a");
    int n;
    for(n = STEP; n < N; n += STEP) {
        fillArray(arrayOriginal, n, 9*n/10);
        double t_isort = useIsort(arrayOriginal, n);
        double t_ssort = useSsort(arrayOriginal, n);
        double t_msort = useMsort(arrayOriginal, n);
        double t_qsort = useQsort(arrayOriginal, n);
        fprintf(pFile, "Size = %d, t_isort = %.6f, t_ssort = %.6f, t_msort = %.6f, t_qsort = %.6f \n", n, t_isort, t_ssort, t_msort, t_qsort);
        printf("Size = %d, t_isort = %.6f, t_ssort = %.6f, t_msort = %.6f, t_qsort = %.6f \n", n, t_isort, t_ssort, t_msort, t_qsort);
    }
    printf("\n\n");
    fclose(pFile);
}

double useIsort(int arO[], int n) {
    copyInto(arO, arrayToSort, n);
    struct timeval t1, t2;
    gettimeofday(&t1, NULL);
    isort(arrayToSort, n);
    gettimeofday(&t2, NULL);
    double timediff = (t2.tv_sec - t1.tv_sec) * 1000.0;      // sec to ms
    timediff += (t2.tv_usec - t1.tv_usec) / 1000.0;   // us to ms
    return timediff;
}

double useSsort(int arO[], int n) {
    copyInto(arO, arrayToSort, n);
    struct timeval t1, t2;
    gettimeofday(&t1, NULL);
    ssort(arrayToSort, n);
    gettimeofday(&t2, NULL);
    double timediff = (t2.tv_sec - t1.tv_sec) * 1000.0;      // sec to ms
    timediff += (t2.tv_usec - t1.tv_usec) / 1000.0;   // us to ms
    return timediff;
}

double useMsort(int arO[], int n) {
    copyInto(arO, arrayToSort, n);
    struct timeval t1, t2;
    gettimeofday(&t1, NULL);
    msort(arrayToSort, n);
    gettimeofday(&t2, NULL);
    double timediff = (t2.tv_sec - t1.tv_sec) * 1000.0;      // sec to ms
    timediff += (t2.tv_usec - t1.tv_usec) / 1000.0;   // us to ms
    return timediff;
}

double useQsort(int arO[], int n) {
    copyInto(arO, arrayToSort, n);
    struct timeval t1, t2;
    gettimeofday(&t1, NULL);
    qisort(arrayToSort, n);
    gettimeofday(&t2, NULL);
    double timediff = (t2.tv_sec - t1.tv_sec) * 1000.0;      // sec to ms
    timediff += (t2.tv_usec - t1.tv_usec) / 1000.0;   // us to ms
    return timediff;
}

int main() {

    testReport();
    return 0;
}

但是編譯器給我以下錯誤:

  • SortingAlgorithmTest.c:95:8:錯誤:'useIsort'的類型沖突double useIsort(int arO [],int n){^
  • SortingAlgorithmTest.c:77:20:注意:之前'useIsort'的隱式聲明是double t_isort = useIsort(arrayOriginal,n); ^
  • SortingAlgorithmTest.c:112:8:錯誤:'useSsort'的類型沖突double useSsort(int arO [],int n){^
  • SortingAlgorithmTest.c:78:20:注意:之前'useSsort'的隱式聲明為double t_ssort = useSsort(arrayOriginal,n); ^
  • SortingAlgorithmTest.c:129:8:錯誤:'useMsort'的類型沖突double useMsort(int arO [],int n){^
  • SortingAlgorithmTest.c:79:20:注意:之前'useMsort'的隱式聲明為double t_msort = useMsort(arrayOriginal,n); ^
  • SortingAlgorithmTest.c:146:8:錯誤:“ useQsort”的類型沖突double useQsort(int arO [],int n){^
  • SortingAlgorithmTest.c:80:20:注意:之前'useQsort'的隱式聲明是double t_qsort = useQsort(arrayOriginal,n); ^

我認為這是一個愚蠢的錯誤,但我認為這是一個小時,我找不到錯誤。 誰能幫我? 謝謝

在C中,當您調用函數時,其定義必須在調用者函數之上。

嘗試將您的use * sort放在testReport()上方,它應該可以解決您的問題。

如果您不介意函數的順序,也可以將所有函數定義復制到.h中。

在testReport中使用函數之前,請聲明它們。

暫無
暫無

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

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