我有stat.hC++ 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? 我认为此代码应工作,我想不通为什么它不编译。任何人都可以发现我做错了什么吗?
操作系统和编译器? – mikithskegg 2012-02-14 21:22:37
虽然我使用了正确的路径分隔符,但我遇到了同样的问题。我在Windows上使用MinGW进行编译,编译器有sys/stat.h文件,其中声明了struct stat和function stat。还有什么想法? – Youda008 2014-08-19 10:51:51