I need to get functionA as a global function in functionB but to use only one instance of class A defined in main(). functionA will use in many classes and I'll want to get access as simple as it possible. I know defining of class in .h file is a bad practise but I think it's not critical in this case. In my case I get error, but how can I do this in the same manner?
If I place A a();
before main()
this works perfect, but A is initialized in the main()
function.
/////A.h//////
class A
{
public:
A(){//define constructor A//};
functionA()
{
...
}
};
/////B.h//////
extern A a; //declare A//
class B
{
public:
B(){//define constructor B//};
functionB()
{
a.functionA();
}
};
////main.cpp////
#include "A.h"
#include "B.h"
int main()
{
B b(); //B b;
A a(); //A a; of course. Thanks for remark guys.
}
This is the error I am getting:
Error LNK2001: unresolved external symbol "class A a"