I declared a struct
within the same .cpp
file of the main()
function. Then no error occurs.
But the error of "incomplete type is not allowed" occurs if I put the struct
in another .cpp
file.
file2.cpp:
struct a_point {
float x, y;
};
file2.h :
struct a_point;
file1.cpp :
include "file2.h"
int main(){
a_point pnt_arr[5]; // error occurs, in visual studio 2017
return 0;
}
CodePudding user response:
Struct definitions are only in the header file.
file2.h :
struct a_point {
float x, y;
};
Delete the content in file2.cpp and no change is needed in file1.cpp.
CodePudding user response:
You are forward declaring the struct in file2.h
. When file1.cpp
includes that header, the compiler only knows that the struct exists, but not what it looks like, and as such it can't create any instances of the struct. By moving the full declaration of the struct into the header file, that is no longer an issue.