【问题标题】:C++ stat.h incomplete type and cannot be definedC++ stat.h 类型不完整,无法定义
【发布时间】:2012-03-06 06:08:44
【问题描述】:

我对 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'

来自这个帖子:How can I get a file's size in C? 我认为这段代码应该可以工作,但我不知道为什么它不能编译。谁能发现我做错了什么?

【问题讨论】:

  • 我遇到了同样的问题,尽管我使用了正确的路径分隔符。我在 Windows 上使用 MinGW 进行编译,编译器有 sys/stat.h 文件,其中声明了 struct stat 和 function stat 。还有什么想法吗?

标签: c++ filesize stat incomplete-type


【解决方案1】:

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

UNIX 手册页:

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

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

如果您使用的是 Windows(我猜您可能是因为 \ 的原因),那么我无能为力,因为我什至不知道有 stat

【讨论】:

  • 啊,问题是代码的 include 部分中的 \ 而不是 /。修复之后,问题就解决了。
猜你喜欢
  • 1970-01-01
  • 2012-01-08
  • 2014-05-20
  • 2012-07-29
  • 2021-06-24
  • 1970-01-01
  • 2020-02-21
  • 1970-01-01
相关资源
最近更新 更多