2016-09-18 53 views
1

好像功能qwertyInches()應該工作,但是當我把它在main()它給了我爲什麼調用這個函數會產生錯誤「<function>不是一個函數或函數指針」?

[錯誤]調用的對象「qwertyInches」不是一個函數或函數指針。

任何幫助將不勝感激。

#include <stdio.h> 
#include <stdlib.h> 
#include <string.h> 
// Global constant 
const int MAX_LENGTH = 81; // global constant for max input line length 


void qwertyInches (char row[], double *inches, int x, double y) { 
    int d; 
    for (d = 0; d < strlen(row); d++) { 
     if (x == row[d]) { 
      *inches = *inches + y; 
     } 
    } 
} 


int main() { 
    int count[256] = { 0 }; 
    int letterCounter = 0; 
    int qwertyCounter = 0; 
    int homeRowCounter = 0; 
    int dvorakCounter = 0; 
    char qwertyHomeRow[23] = {"asdfghjkl;ASDFGHJKL:\"'" }; 
    char dvorakHomeRow[22] = {"aoeuidhtns-_AOEUIDHTNS"}; 
    double percentOfCharQwerty = 0.0; 
    double percentOfCharDvorak = 0.0; 
    char qwertyHomeRowInches[4] = {"ghGH"}; 
    char qwertyRowInches[46] = {"qweruiopQWERUIOP{[}]\ZzXx|CcVvNnMm<,>./?"}; 
    char qwertyNumberInches[25]= {"`[email protected]#4$5%7&8*9(0)-_=+)"}; 
    char qwertyTAndYInches[4] = {"TtYy"}; 
    char num6Inches[2] = {"6^"}; 
    char dvorakHomeRowInches[4]= {"iIDd"}; 
    char dvorakRowInches[41] = {"\"<',.>PpGgCcRrLl?/:+=|:;QqJjKkBb\MmWwVvZz"}; 
    char dvorakYandFInches[4] = {"YyFf"}; 
    char dvorakNumberInches [25] = {"~`[email protected]#4$5%7&8*9()0{[]}"}; 
    double dvorakInches = 0.0; 
    double qwertyInches = 0.0; 


    /* loop counters */ 
    int k; 
    int l; 
    int d; 
    /* file handle --- in this case I am parsing this source code */ 
    FILE *fp = fopen("newsample.txt", "r"); 

    /* a holder for each character (stored as int) */ 
    int c; 

    /* for as long as we can get characters... */ 
    while((c=fgetc(fp))) { 

     /* break if end of file */ 
     if(c == EOF) { 
      break; 
     } 
     else if (c == 32 || c == 10 || c == 9) { 
      count[c]+=1; 
     } 
     /* otherwise add one to the count of that particular character */ 
     else { 
      count[c]+=1; 
      letterCounter++; 
      for (l = 0; l < strlen(dvorakHomeRow); l++) { 
       if (c == qwertyHomeRow[l]) { 
        qwertyCounter++; 
       } 
       if (c == dvorakHomeRow[l]) { 
        dvorakCounter++; 
       } 
      } 
      qwertyInches(strlen(qwertyHomeRowInches) , &qwertyInches, c, .75); 

     } 

    } 


percentOfCharQwerty = (double) qwertyCounter/(double) letterCounter * 100; 
percentOfCharDvorak = (double) dvorakCounter/(double) letterCounter * 100; 

printf("Amount of Letters: %d\n", letterCounter); 
printf("qwerty counter: %d\n", qwertyCounter); 
printf("Dvorak counter: %d\n", dvorakCounter); 
printf("Percent of qwerty letters %.2lf\n", percentOfCharQwerty); 
printf("Percent of Dvorak letters %.2lf\n", percentOfCharDvorak); 
printf("qwertyInches: %.2lf\n", qwertyInches); 
printf("dvorakInches: %.2lf\n", dvorakInches); 
/* close the file */ 
fclose(fp); 
return; 
} 
+1

在許多答案中,它也看起來像你傳遞了錯誤的參數。你有'char row []'是第一個參數,但傳入'strlen(qwertyHomeRowInches)',這是一個int; –

+0

@MichaelPickett我的意思是'size_t',對吧? –

+0

@SouravGhosh是的,我做到了。我的錯。 –

回答

4

main()裏面有一個qwertyInches局部變量,其中陰影qwertyInches()函數。

引用C11,章§6.2.1,作用域標識符的(重點礦山

[....如果標識符指定在相同的名稱 空間兩個不同的實體,範圍可能會重疊。如果是這樣,一個實體的(內範圍)的範圍將其他實體(外部範圍)的範圍之前嚴格結束 。 在內部範圍內, 標識符指定在內部範圍內聲明的實體;在外部 範圍內聲明的實體在內部範圍內隱藏(並且不可見)。

解決方案:更改名稱之一。

這就是說,qwertyInches()函數的第一個參數應該是一個char *,但你傳遞一個size_t(的strlen()輸出),這是完全錯誤的。改變這一點。

1

您將qwertyInches定義爲變量和函數。

void qwertyInches (char row[], double *inches, int x, double y) { 

double qwertyInches = 0.0; 

只要改變上述之一的名稱。我通常給我的函數提供一個「action」名稱,併爲我的變量賦予一個「thing」名稱。

2

當我在main調用它時,它給了我這個... [錯誤]稱爲對象'qwertyInches'不是函數或函數指針。

當然,編譯器是正確的。 內部main(),此聲明陰影函數聲明:

double qwertyInches = 0.0; 

因此,在main()qwertyInches指型double,不是一個函數的一個局部變量。

相關問題