簡體   English   中英

C的新手……在未完成的函數聲明上獲取“ UArray2.h:19:錯誤:在'*'標記前應有預期的')'”

[英]New to C… getting “UArray2.h:19: error: expected ‘)’ before ‘*’ token” on an unfinished function declaration

我正在為一個學校的CS項目工作,但是我似乎遇到了這個奇怪的錯誤,這是毫無幫助的。 我和我的伴侶似乎無法弄清楚。

它在第一個函數聲明我的.h文件(UArray2_new)上顯示“ UArray2.h:18:錯誤:預期的'=',',',';','asm'或' attribute '在'*'標記之前”。

在每條帶有函數聲明的行上都重復出現“錯誤:在'*'標記前的預期錯誤)。 有任何想法嗎? 我在我的主機中包含一個空白主文件,因此您可以看到它不可能是編譯問題。 我使用“ gcc -Wall -Wextra -Werror UArray2.c”進行編譯。

//UArray2.h                                                                  
#include <stdlib.h>
#include <stdio.h>


#ifndef UARRAY2_INCLUDED
#define UARRAY2_INCLUDED

struct UArray2_T{
    int width, height;                                                     
};   

extern UArray2_T * UArray2_new(int height, int width, int size);
extern void UArray2_free(UArray2_T * uarray2);                                                                
extern void * UArray2_at(UArray2_T * uarray2, int column, int row);
extern int UArray2_size(UArray2_T * uarray2);
extern int UArray2_columns(UArray2_T * uarray2);
extern int UArray2_rows(UArray2_T * uarray2);
static int UArray2_index(UArray2_T * uarray2, int col, int row);

#endif

這是.c文件

//UArray.c                                                                          
#include <stdlib.h>
#include <stdio.h>
#include "UArray2.h"                                                             

int main()
{

     return 0;
}

UArray2_T * UArray2_new(int height, int width, int size)
{
    T newArray=malloc(sizeof(UArray2_T));
    newArray->height=height;
    newArray->width=width;
    (void) size;                            
    return newArray;
}

void UArray2_free(UArray2_T * uarray2)
{
    (void) uarray2;
    //need to write                                                                 

}

void * UArray2_at(UArray2_T * uarray2, int column, int row)
{

    (void) uarray2;
    int index=UArray2_index(uarray2, column, row);
    (void) index;
    //if(index>0 && index<=column*row)                                              
    //return UArray_at(uarray2->UArray, index);                                     
    //else                                                                          
    //throw an error                                                                
    char * k="hi"; //dummy variables
    return k;
}

int UArray2_size(UArray2_T * uarray2)
{
    //UArray_size(uarray2->UArray);                                                 
    return 0;
}
int UArray2_columns(UArray2_T * uarray2){
    return uarray2->width;
}

到處都有UArray2_T * ,應將其替換為struct UArray2_T * 當您引用struct類型時, struct關鍵字對於C是必需的(但在C++ 不是 )。

避免這種情況的標准方法是使用typedef ,例如:

typedef struct {
    int width, height;                                                     
} UArray2_T;

在C,稱struct Foo 不會引入新的類型,稱為Foo 該類型稱為struct Foo

您可以為類型名稱創建別名,如下所示:

typedef struct UArray2_T UArray2_T;

這樣做應該可以解決您的錯誤。

我將結構移到.c文件。 這將隱藏結構的內部。

struct UArray2_T { // move to the .c file
    int width, height;                                                     
};

將前向聲明添加到.h文件,如下所示:

struct UArray2_T;

您需要typedef您的結構,或在任何地方使用struct關鍵字。 我喜歡這種風格:

typedef struct {
    int data1;
    char data2;
} newtype;

暫無
暫無

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

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