不完整的类型,无法定义。

时间:2022-11-06 16:09:11

I am having a very strange issue with stat.h

我和他有一个很奇怪的问题。

At the top of my code, I have declarations:

在我的代码顶部,我有声明:

#include <sys\types.h> 
#include <sys\stat.h> 

And function prototype:

和函数原型:

int FileSize(string szFileName);

Finally, the function itself is defined as follows:

最后,函数本身定义如下:

int FileSize(string szFileName)
{
  struct stat fileStat; 
  int err = stat( szFileName.c_str(), &fileStat ); 
  if (0 != err) return 0; 
  return fileStat.st_size;
}

When I attempt to compile this code, I get the error:

当我试图编译这段代码时,我得到了错误:

divide.cpp: In function 'int FileSize(std::string)':
divide.cpp:216: error: aggregate 'stat fileStat' has incomplete type and cannot be defined
divide.cpp:217: error: invalid use of incomplete type 'struct stat'
divide.cpp:216: error: forward declaration of 'struct stat'

From this thread: How can I get a file's size in C? I think this code should work and I cannot figure out why it does not compile. Can anybody spot what I am doing wrong?

从这个线程:我如何在C中得到一个文件的大小?我认为这段代码应该有效,我不知道为什么它不能编译。有人知道我做错了什么吗?

1 个解决方案

#1


5  

Are your \'s supposed to be /'s or am I just confused about your environment?

你的是应该是/还是我只是对你的环境感到困惑?

UNIX MAN page:

UNIX手册页:

 #include <fcntl.h>
 #include <sys/types.h>
 #include <sys/stat.h>

 int stat(const char *restrict path, struct stat *restrict buf);

If you're on Windows (which I'm guessing you might be because of the \'s), then I can't help because I didn't even know that had stat.

如果你是在Windows上(我猜你可能是在Windows操作系统中),那么我就无能为力了,因为我甚至不知道有这个数据。

#1


5  

Are your \'s supposed to be /'s or am I just confused about your environment?

你的是应该是/还是我只是对你的环境感到困惑?

UNIX MAN page:

UNIX手册页:

 #include <fcntl.h>
 #include <sys/types.h>
 #include <sys/stat.h>

 int stat(const char *restrict path, struct stat *restrict buf);

If you're on Windows (which I'm guessing you might be because of the \'s), then I can't help because I didn't even know that had stat.

如果你是在Windows上(我猜你可能是在Windows操作系统中),那么我就无能为力了,因为我甚至不知道有这个数据。