C++ stat.h不完整类型无法定义。

7

我可以帮您进行翻译。以下是关于it技术的内容:

我的代码中出现了一个非常奇怪的stat.h问题。

在代码顶部,我有这些声明:

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

函数原型:

int FileSize(string szFileName);

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

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

当我尝试编译这段代码时,出现了错误:

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'

来自这个帖子:如何在C语言中获取文件大小? 我认为这段代码应该是有效的,但我无法弄清楚为什么它不能编译。有人能发现我做错了什么吗?


我遇到了同样的问题,尽管我使用了正确的路径分隔符。我在Windows上使用MinGW编译器,并且编译器有sys/stat.h文件,其中声明了struct stat和函数stat。还有更多的想法吗? - Youda008
1个回答

6

您的\应该是/吗?还是我对您的环境存在误解?

UNIX MAN页面:

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

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

如果你使用的是Windows操作系统(我猜你可能是因为出现了\字符),那么我无法提供帮助,因为我甚至不知道Windows有stat这个命令。


1
啊,问题在于代码中的include部分使用了\而不是/。修复后,问题得到解决。 - user788171

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接