2012-02-14 85 views
6

我有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? 我认为此代码应工作,我想不通为什么它不编译。任何人都可以发现我做错了什么吗?

+1

操作系统和编译器? – mikithskegg 2012-02-14 21:22:37

+0

虽然我使用了正确的路径分隔符,但我遇到了同样的问题。我在Windows上使用MinGW进行编译,编译器有sys/stat.h文件,其中声明了struct stat和function stat。还有什么想法? – Youda008 2014-08-19 10:51:51

回答

5

你的\应该是/的还是我只是混淆了你的环境?

UNIX手册页:

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

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

如果您使用的是Windows(我猜你可能是因为\的),那么我不能帮助,因为我根本就没知道有stat

+1

是不必要的 – mikithskegg 2012-02-14 21:32:14

+0

啊,问题是\而不是代码中的包含部分。解决之后,问题就解决了。 – user788171 2012-02-14 21:45:48