I define the struct A in the a.cpp in the first.
// a.cpp
#include"a.h"
struct A {
A(int num):a(num){}
int a;
};
A obj{1};
A* obj_ptr = &obj;
and in the header, i made the forward declaration of A and declaration of obj_ptr:
// a.h
struct A;
extern A* obj_ptr;
then i want to use obj in another cpp file called b.cpp, but compiler reported error: invalid use of incomplete type "struct A"
// b.cpp
#include<iostream>
#include"a.h"
int main(){
std::cout << obj_ptr->a << std::endl;
}
One way to fix it is to move the definition of A from a.cpp to a.h, but I wonder if there is any way else to fix it?