c - 错误: expected specifier-qualifier-list before "File"

标签 c debugging compiler-errors

我为另一个主程序编写了这个头文件。该文件声明了一个由 main 函数(其他地方)使用的 struct_buffer 结构。

我想做的是,当有人使用 buffer_init 函数初始化缓冲区时,会返回指向缓冲区的指针。缓冲区将包含一个指针数组、缓冲区中当前指针的数量(大小)以及指向存储在缓冲区中的指针将转储到的文件的文件指针。

主程序将调用add_to_buffer()函数向缓冲区添加指针。这又调用 buffer_dump() 函数将指针转储到 buffer->fp 指定的文件中。最后,我将调用 buffer_close() 来关闭文件。然而,编译它给了我一些我无法理解和消除的错误。

以下是 C 头文件的代码,我正在尝试编译它,但它给了我我无法理解的错误。

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define MAX_PTRS 1000

struct struct_buffer
{
    int size;
    File *fp;
    unsigned long long ptrs[MAX_PTRS];
};

//The struct_buffer contains the size which is the number of pointers in ptrs, fp is the file pointer to the file that buffer will write 
struct struct_buffer*
buffer_init(char *name)
{
    struct struct_buffer *buf = malloc(sizeof(struct struct_buffer));
    buf->size = 0;
    buf->fp = fopen(name,"w");
    return buf;
}

//This function dumps the ptrs to the file.
void
buffer_dump (struct struct_buffer *buf)
{
  int ctr=0;
  for(ctr=0; ctr < buf->size ; ctr++)
  {
    fprintf(buf->fp, "%llx\n",buf->ptrs[ctr]);
  }
}

//this function adds a pointer to the buffer
void
add_to_buffer (struct struct_buffer *buf, void *ptr)
{
 if(buf->size >= MAX_PTRS)
 {
  buffer_dump(buf);
  buf->size=0;
 }
 buf->ptrs[(buf->size)++] = (unsigned long long)ptr;
}

//this functions closes the file pointer
void
buffer_close (struct struct_buffer *buf)
{
 fclose(buf->fp);
}                          

上面的编译代码给出了以下错误。我无法理解这个问题。请向我解释一下问题所在。

buffer.h:10: error: expected specifier-qualifier-list before 'File'
buffer.h: In function 'buffer_init':
buffer.h:19: error: 'struct struct_buffer' has no member named 'fp'
buffer.h: In function 'buffer_dump':
buffer.h:29: error: 'struct struct_buffer' has no member named 'fp'
buffer.h:29: error: 'struct struct_buffer' has no member named 'ptrs'
buffer.h: In function 'add_to_buffer':
buffer.h:40: error: 'struct struct_buffer' has no member named 'ptrs'
buffer.h: In function 'buffer_close':
buffer.h:46: error: 'struct struct_buffer' has no member named 'fp'

最佳答案

文件 未定义。您正在寻找的正确类型是FILE

struct struct_buffer
{
    int size;
    FILE *fp; // <---------
    unsigned long long ptrs[MAX_PTRS];
};

您的其余错误似乎是此错误的产物,因此这应该是您唯一的修复方法。

关于c - 错误: expected specifier-qualifier-list before "File",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7522741/

相关文章:

c - 编译示例 CUDA 程序时出现的问题

c - C语言中signed char和char有什么区别?

c - 不使用条件语句检测-1

angularjs - 使用 Ionic 框架在模拟器中进行调试

eclipse - 在一个项目中开发和调试 Eclipse Web 应用程序

c - 'Table1' 未声明(在此函数中首次使用)

iphone - 如何在 iphone 本身上访问我的 iphone 的日志文件

c# - 将程序集添加到gac时csharpcodeprovider无法编译

c++ - Arduino 错误 (C++) : invalid use of non-static data member

ios - 参数类型 'customClass.Type' 不符合预期类型 'NSItemProviderWriting'