PDA

View Full Version : multiple definitoin of variable



saman_artorious
22nd April 2013, 15:43
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?

Santosh Reddy
22nd April 2013, 15:55
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.

BalaQT
22nd April 2013, 16:26
For extern variable:
ex: extern QList mylist;

For
but I constantly receive multiple definition of variable xx.
Use header guards,
Ex:
#ifndef GLOBAL_H
........
#endif

hope it helps,
Bala

Santosh Reddy
22nd April 2013, 19:39
For

but I constantly receive multiple definition of variable xx.
Use header guards,
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/error

amleto
22nd April 2013, 19:50
I need to define my queue globally in a header file.
no, you don't.

saman_artorious
22nd April 2013, 20:32
no, you don't.

if queue is defined privately in my class, other object cannot get access to it and enQ. so, I have to set it as global. I cannot pass the QByteArray to the producer of the thread, so that it enQ it to the queue.

wysota
22nd April 2013, 20:47
if queue is defined privately in my class, other object cannot get access to it and enQ. so, I have to set it as global.
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.