Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ stat.h incomplete type and cannot be defined

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?

like image 452
user788171 Avatar asked Feb 14 '12 21:02

user788171


1 Answers

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

UNIX MAN page:

 #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.

like image 162
John Humphreys Avatar answered Nov 03 '22 04:11

John Humphreys