3

I would like to know how to fix this error:

error: aggregate 'Padron_Electoral::getFileSize(const char*)::stat stat_buf' has incomplete type and cannot be defined

The method in question is:

long long const Padron_Electoral::getFileSize(const char* filename) {

     struct stat stat_buf; // This line doesnt compile en the computer Im currently working on, If tried on another computer this method compiles and work.
     int rc = stat(filename, &stat_buf);
    return rc == 0 ? stat_buf.st_size : -1;
}

If I comment this method the rest of the code compiles, Im using GNU GCC compiler. Both computers have the same compiler and Im working in Codeblocks. If I try to compile it on console it throws the same error. Has anyone got this same mistake? What could be the reasons and how to fix it?

Code is identical in both computers, #include #include and the include of the .h file

Other errors it throws:

error: invalid use of incomplete type 'struct Padron_Electoral::getFileSize(const char*)::stat'

error: forward declaration of 'struct Padron_Electoral::getFileSize(const char*)::stat'

The #include could be included on the computer where the code compiled but on the other computer the include throws errors:

Build: Debug in Prueba (compiler: GNU GCC Compiler) c:\mingw\include\io.h|301|error: 'off64_t' does not name a type c:\mingw\include\io.h|302|error: 'off64_t' does not name a type c:\mingw\include\unistd.h|65|error: 'off_t' has not been declared| C:\Users\Gabriel\Documents\Progra II\Prueba\main.cpp||In function 'int main()': C:\Users\Gabriel\Documents\Progra II\Prueba\main.cpp|7|error: aggregate 'main()::stat stat_buf' has incomplete type and cannot be defined| C:\Users\Gabriel\Documents\Progra II\Prueba\main.cpp|8|error: invalid use of incomplete type 'struct main()::stat'| C:\Users\Gabriel\Documents\Progra II\Prueba\main.cpp|7|error: forward declaration of 'struct main()::stat' Build failed: 6 error(s), 0 warning(s) (0 minute(s), 0 second(s))

Operating system of the computer where the code fails to compile is: Windows 7 Ultimate.

Processor of the computer where the code fails to compile: Intel Core i5 4440 CPU 3.10GHz

MinGW. GCC 4.8.1 (both computers)

IDE: Codeblocks 13.12 (both machines use this IDE)

Both are 64 bit Operating Systems

Operating system of the computer where it compiles and works: Windows 7 Home Premium.

Processor of the computer where it compiles and works: Intel Pentium CPU P6100 2.0GHz

Thank you

Edit: Code compiles now, MinGW32 issue

long long const Padron_Electoral::getFileSize(const char* filename) {
#if __MINGW32__
    struct __stat64 stat_buf;
    int rc = __stat64(filename, &stat_buf);
    return rc == 0 ? stat_buf.st_size : -1;

#else
struct stat stat_buf;
int rc = stat(filename, &stat_buf);
return rc == 0 ? stat_buf.st_size : -1;
#endif
return 0;
}

Question remains on why does MinGW throws an error on this method on one computer and not in the other.

GabrielCB
  • 53
  • 1
  • 8
  • 5
    Do you have `#include `? – Brian Bi Jul 20 '14 at 20:29
  • yep, the code is the same in both computers. I also did the include to the .h file – GabrielCB Jul 20 '14 at 20:33
  • Try sys/types.h and/or unistd.h – kayrick Jul 20 '14 at 20:39
  • Are there any other errors, or is that the only one? – jxh Jul 20 '14 at 20:40
  • Older posixes required `#include `. – avakar Jul 20 '14 at 20:41
  • tried with sys/types.h and it throws the same mistake. If I also include unistd.h it enters io.h and throws an error there. – GabrielCB Jul 20 '14 at 20:42
  • If the entire method is tried it also throws error: invalid use of incomplete type 'struct Padron_Electoral::getFileSize(const char*)::stat'| error: forward declaration of 'struct Padron_Electoral::getFileSize(const char*)::stat'| – GabrielCB Jul 20 '14 at 20:43
  • Take out the word `struct`. It isn't needed in C++, and it is causing declaration of an incomplete type locally inside the function, which is obscuring the real error that you don't have a proper declaration of the real `struct stat` in scope. – Ben Voigt Jul 20 '14 at 20:51
  • If I remove struct then stat wouldnt be declared in this scope. The method works on another computer in the same program, when the code is moved to another computer it throws those errors. – GabrielCB Jul 20 '14 at 20:56
  • @GabrielCB: _"stat wouldnt be declared in this scope"_ - why not? Did you try it? On both compilers? If not, just give it a try and tell us what you see. – Component 10 Jul 20 '14 at 21:00
  • @Component10 yes, I tried before on the computer Im working and now on the other one, on both of them it throws errors. – GabrielCB Jul 20 '14 at 21:16
  • @Component10 on the computer where the code works when I remove struct at the beginning and stay with stat stat_buf; it throws error: expected ';' before 'stat_buf'| and error: 'stat_buf' was not declared in this scope| – GabrielCB Jul 20 '14 at 21:28
  • on the computer where the code doesnt compile it throws: error: 'stat' was not declared in this scope ---- error: expected ';' before 'stat_buf'| ---- and error: 'stat_buf' was not declared in this scope| – GabrielCB Jul 20 '14 at 21:30
  • Ah! This is because of the ambiguity mentioned [here](http://stackoverflow.com/questions/8422775/why-does-c-need-struct-keyword-and-not-c/#8423005) The only other thing I could suggest is to try `typename ::stat stat_buf` but I don't really see why you should have to do that. – Component 10 Jul 20 '14 at 21:56
  • in the same place where the method is or elsewhere? If its in the method I put struct typename ::stat stat_buf; It didnt compiled, and typename ::stat stat_buf; and still it didnt work. Yeah I dont get it either, its quite weird because its just the struct line that gives trouble, the rest of the code in the program works fine, I tried reinstalling the compiler but the issue persisted. Thanks for the suggestions, I was hoping it would work XD, but damn, what worries me is that I need to work on the computer where the code doesnt compile, the other one is my sister´s laptop :/ – GabrielCB Jul 20 '14 at 22:45
  • What worries me is that if something doesnt compile I wont be sure if its the same issue as here or a normal compiler error. – GabrielCB Jul 20 '14 at 22:47
  • Is it **sure** that there is `struct stat` in , which your problemed-compiler refers? system headers are sometimes broken; – ikh Jul 20 '14 at 23:42
  • "stat wouldnt be declared in this scope" -- that was the real error being obscured. Do take out the `struct` if this is C++ code. – Ben Voigt Jul 21 '14 at 03:45
  • If I remove struct then it fails to compile, apparently its because of an ambiguity, @Component10 nailed it. By the way, should I post the answer and close it and/or how do I close it? – GabrielCB Jul 21 '14 at 03:52
  • Instead of editing the title to say "Solved", accept an answer. (If nobody has written a correct answer yet , then write your own answer and accept it). – M.M Jul 21 '14 at 04:48
  • Ok, great as soon as the 10 hours limit pass Ill post the answer – GabrielCB Jul 21 '14 at 05:06

2 Answers2

2

Where is the struct stat defined?

The compiler is telling you he couldn't find the definition of stat, and therefore stat_buf has an incomplete type (wich means it can only be defined as a pointer).

What it implies is that there is some difference at compile time between the two computers where you are compiling.

To pinpoint the problem, you should try to isolate the problem in a simpler program and try to compile it and run it on both systems. Something like below could be enough:

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

int main()
{
    struct stat stat_buf; // This line doesnt compile en the computer Im currently working on, If tried on another computer this method compiles and work.
    int rc = stat("testfile.txt", &stat_buf);
    return rc == 0 ? stat_buf.st_size : -1;
}

It is a complete compiling program, and you should be able to post in the question the compiling errors you get on both target systems.

Explicitely stating both target systems (which obviously are not identical) could also help: - compiler version - operating system - processor - actual compilation line and/or makefile if possible.

For instance such problems could arise because of some missing define like _POSIX_C_SOURCE

Spoting such problems is usually easy enough if you give enough informations.

EDIT: After checking it appears that the problem was related to mingw. Specifically using MingW32 on a 64 bits system. A possible solution was available here: 64 bits file size on Mingw32

Community
  • 1
  • 1
kriss
  • 23,497
  • 17
  • 97
  • 116
  • @cassiorenan: I mostly rephrased your (deleted) answer to make it look like more like an actual answer. At first I intended to rephrase your answer by editing it, but it was deleted before I may do that. – kriss Jul 20 '14 at 23:27
  • Hi, ok, Ill add the missing information. The unistd.h include doesnt work when I try it, I suspect it could be Im using MinGW for Windows but its just a wild guess. I tried the code above and it didnt compile on my current machine, Ill try it later on the one where the initial code did compile and worked. – GabrielCB Jul 20 '14 at 23:43
  • Well, the code compiled on the machine where my code compiles and on the computer it doesnt it throws: c:\mingw\include\io.h|301|error: 'off64_t' does not name a type| c:\mingw\include\io.h|302|error: 'off64_t' does not name a type| c:\mingw\include\unistd.h|65|error: 'off_t' has not been declared| error: aggregate 'main()::stat stat_buf' has incomplete type and cannot be defined| |error: invalid use of incomplete type 'struct main()::stat'| error: forward declaration of 'struct main()::stat' – GabrielCB Jul 21 '14 at 00:02
  • The #include could be included on the computer where the code compiled but on the other computer the include throws errors. – GabrielCB Jul 21 '14 at 00:06
  • 1
    The problem is very likely related to mingw. Do you use MingW32 on a 64 bits system ? If so the problem may be related to this one: http://stackoverflow.com/questions/12539488/determine-64-bit-file-size-in-c-on-mingw-32-bit – kriss Jul 21 '14 at 00:26
  • The headers I included are standard Linux ones (got them from the man page). But Mingw may have different requirements. – kriss Jul 21 '14 at 00:27
  • 1
    Also see here http://sourceforge.net/p/mingw/bugs/2104/ you could try to #undef _____STRICT_ANSI_____ this define may be setted automagically by your compilation environment, and if you have it, it seems some types necessary for stat structures are not defined. – kriss Jul 21 '14 at 00:31
  • Great!@kriss thanks, yep Im using MingW32, I thought the other computer had the same, the code compiles and works with long long const Padron_Electoral::getFileSize(const char* filename) { #if __MINGW32__ struct __stat64 stat_buf; int rc = __stat64(filename, &stat_buf); return rc == 0 ? stat_buf.st_size : -1; #else struct stat stat_buf; // This line doesnt compile en the computer Im currently working on, If tried on another computer this method compiles and work. int rc = stat(filename, &stat_buf); return rc == 0 ? stat_buf.st_size : -1; #endif return 0; } – GabrielCB Jul 21 '14 at 03:38
  • How do i recognize, if i have MinGW32 ? I downloaded just MinGW (without classification), and i'm facing the same problem with stat and off64_t. – Youda008 Aug 19 '14 at 14:31
  • There is 32 or 64 in the package name. On SourceForge mingw is 32 bits version while version 64 bits is mingw-w64 (supposed to work on both 32 and 64 bits). – kriss Aug 19 '14 at 16:07
2

Code compiles now, MinGW 32 bits on a 64 bits issue. Hope this helps someone else

long long const Padron_Electoral::getFileSize(const char* filename) {
#if __MINGW32__
struct __stat64 stat_buf;
int rc = __stat64(filename, &stat_buf);
return rc == 0 ? stat_buf.st_size : -1;

#else
 struct stat stat_buf;
 int rc = stat(filename, &stat_buf);
 return rc == 0 ? stat_buf.st_size : -1;
 #endif
 return 0;
}

Thank you all for the help and specially to @kriss , answer based on Determine 64-bit file size in C on MinGW 32-bit

Community
  • 1
  • 1
GabrielCB
  • 53
  • 1
  • 8
  • this has solved only undeclared stat, but the problem with "off64_t' does not name a type" still remains. – Youda008 Aug 19 '14 at 11:31