2010-09-04 68 views
1

標題幾乎可以說明一切。這似乎是它的根的精確錯誤信息是:C標題在包含在某些文件中時會導致錯誤,但不包括在其他文件中

util.h:4: error: expected declaration specifiers or ‘...’ before ‘size_t’

有問題的標題是:

#ifndef UTIL_H 
#define UTIL_H 

void print_array(void*, int, size_t, void (*)(void*)); 
extern void print_int(void*); 
extern void print_float(void*); 

#endif /* UTIL_H */ 

如果我編譯以下文件與gcc -Wall -c util.c編譯器靜默創建目標文件。

#include <stdio.h> 
#include "util.h" 

void print_array(void* a, int length, size_t size, void (*print)(void*)) { 
    unsigned int i; 
    for (i = 0; i < length; i++) { 
    print(a + i*(unsigned int)size); 
    } 
    printf("\n"); 
} 

void print_int(void* i) { 
    int* a = (int*) i; 
    printf(" %i ", *a); 
} 

void print_float(void* f) { 
    float* a = (float*) f; 
    printf(" %f ", *a); 
} 

如果我將它與任何其他文件包含在一起,我會得到上述錯誤和其他一些錯誤。我提供的是第一個。我在Google上搜索的所有內容都表明,這是前一行語法錯誤的結果,但是當文件中的第一行正在發生。我可以看到,如果我得到這個錯誤被淘汰,那麼所有其他的將會消失,因爲他們必須處理與print_array被調用的錯誤數量或類型的參數(它不是)。

回答

3

size_t直到包含stddef.h才被定義。您的標題應該首先包含該標題以確保其定義。 (就目前而言,你只是變得「幸運」,並且擁有其他包含,最終將其定義爲首先包含它,因此不會導致問題。)

+0

oi這是令人尷尬的。我必須等待十分鐘才能接受你的回答。如果任何人作爲代表備用,我應該得到一個或兩個這樣的倒退。 – aaronasterling 2010-09-04 06:43:47

+1

@aaronasterling,不要流汗。有時候編程的最佳部分是當你在同一時間感覺聰明和愚蠢。 ;-) – 2010-09-04 06:48:21

+0

'size_t'的''呢? – 2011-12-18 16:35:23

相關問題