I need to define my queue globally in a header file. And I of course include that header in other classes.
but I constantly receive multiple definition of variable xx.
how can I declare it only once? so for the next times compiler ignores it?
I need to define my queue globally in a header file. And I of course include that header in other classes.
but I constantly receive multiple definition of variable xx.
how can I declare it only once? so for the next times compiler ignores it?
You need to define the variable in a cpp file (not in header file).
The header file will have just declaration of the variable with "extern" keyword as prefix, and without intilaization.
When you know how to do it then you may do it wrong.
When you don't know how to do it then it is not that you may do it wrong but you may not do it right.
For extern variable:
ex: extern QList mylist;
ForUse header guards,but I constantly receive multiple definition of variable xx.
Ex:
#ifndef GLOBAL_H
........
#endif
hope it helps,
Bala
Using guard macro in header file is a good practice, but it will not solve the multiple definition problem/errorOriginally Posted by BalaQT
When you know how to do it then you may do it wrong.
When you don't know how to do it then it is not that you may do it wrong but you may not do it right.
If you have a problem, CUT and PASTE your code. Do not retype or simplify it. Give a COMPLETE and COMPILABLE example of your problem. Otherwise we are all guessing the problem from a fabrication where relevant details are often missing.
No, you don't have to set it global. But even if you did, you wouldn't do it in a header file as these get included into multiple compilation units causing mutliple declaration problems as the one you're suffering from right now. I think this is a good moment to suggest spending some time with a good book on object-oriented programming before tackling real problems.
Bookmarks