0

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" 
javierMarquez
  • 142
  • 2
  • 10
  • I think you might need to include #include "A.h" at the top of the B.h file. Let me know if this works. – Alex Jul 23 '20 at 17:55
  • 3
    Not the problem but FWIW, `B b();` and `A a();` declare functions in `main`, not objects. You need `B b; A a;` or `B b{}; A a{};` – NathanOliver Jul 23 '20 at 17:55
  • 1
    Is it an option to have a reference to an `A` inside `B` which gets initialised via constructor parameter? – Yunnosch Jul 23 '20 at 17:58
  • 1
    `functionA will use in many classes` Then you'll have to make it `static`, make `a` global, or pass a reference to `a` to functions that need to use its member methods. – dxiv Jul 23 '20 at 18:01
  • 1
    [When to use extern in C++](https://stackoverflow.com/questions/10422034/when-to-use-extern-in-c) covers what's gone wrong here. Also handy reading (even though it's discussing and referencing C): [How do I use extern to share variables between source files?](https://stackoverflow.com/questions/1433204/how-do-i-use-extern-to-share-variables-between-source-files) – user4581301 Jul 23 '20 at 18:01
  • 3
    That said, avoid globals and prefer to pass variables around instead. – user4581301 Jul 23 '20 at 18:01
  • @Alex I tried right now, but get the same error. (( – javierMarquez Jul 23 '20 at 18:02
  • 3
    @javierMarquez `extern A a;` This declares a global object `a`, but you don't define one anywhere, which is what LNK2001 complains about. – dxiv Jul 23 '20 at 18:04
  • 1
    class B probably should take a reference to an `A` object instead of the global variable `a` – drescherjm Jul 23 '20 at 18:08
  • @dxiv Sorry, my mistake, I define it in class A and initialize in main(). Look update. – javierMarquez Jul 23 '20 at 18:24
  • 1
    It must be a global variable not a variable local to main. – drescherjm Jul 23 '20 at 18:32
  • 1
    ***How to get this instance in another class?*** Get rid of the extern a; and the global variable and instead pass a reference to A in the constructor of B store that as a class member in B – drescherjm Jul 23 '20 at 18:33
  • 2
    @javierMarquez `I define it in class A and initialize in main()` No, not at all. What you declare with `extern A a;` in the header file is a *global* object `a`. What you define in `main` is a *local* variable `a` which only happens to have the same name. The two `a`'s are not referring to the same object, and the global object `a` is not defined anywhere. – dxiv Jul 23 '20 at 18:43
  • @dxiv I'm surprised the main () function also has a local variable. Thank you and all you guys. That's why initializing A before main() works fine. – javierMarquez Jul 23 '20 at 20:01
  • 1
    @javierMarquez I assumed that's what you meant there, but `main` does not in fact have a local variable `a`. See Nathan Oliver's comment about why `A a();` is actually parsed as a function declaration. – dxiv Jul 23 '20 at 20:38

0 Answers0