This one is making me scratch my head too long.
I have the following in a header test.h:
inline void anything(){
std::cout<<" anything "<<ii;
}
Then I have a.h, which includes test.h:
class Fooness{
public:
Fooness(){
anything(); //compiler reports "use of undeclared identifier"
};
};
HOWEVER, if I simply move the function definition to a.cpp:
Fooness::Fooness(){
anything();
}
It works. a.cpp includes test.h which includes a.h. Why is anything() only visible in a.cpp not a.h?
As you pointed out in the comments, you included
a.hintest.hand vice versa. This introduces errors as functions and classes being “undefined” because of a cyclic dependency, also known as cross-include.In your case, when a
.cppfile includestest.h, it first includesa.hand then defines the functionanything();, which is obviously not what you want, since when processinga.h,anything()is undefined.Your code expands to something similar to this, when compiling a unit which includes
test.h(beforea.h), which by itself includesa.hbefore anything else:As you see, there is no
anything()defined when you use it. However, if a compilation unit includesa.h(beforetest.h), which itself includestest.h, it expands to something like this:So the order is correct.
To make it work in both situations, you can forward-declare
anything()intest.hbefore you includea.h:Corrected version of test.h:
Then, when including
test.h(beforea.h), it expands to the following: