Click here to Skip to main content
15,936,395 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Hi Everyone there,

I am doing a dialog based application using MFC. My problem looks very simple, but i am getting pages of errors.

I have a class derived from CDialog.

class CSampleDialogLogicDlg : public CDialog

And have two sub classes derived from CSampleDialogLogicDlg.

class CMlkOne : public CSampleDialogLogicDlg 
class CMlkTwo : public CSampleDialogLogicDlg

And I have set of variables in a normal header file. I need to access those variables from both of my subclasses. So i thought, it is enough to include that header file in my base class CSampleDialogLogicDlg header file.

But I am getting Variable already defined error for all the variables.

What should i do to get all those variables in header as global through out project.Am i missing anything here?

Pls help me in this.


[edit]Code blocks, "Ignore HTML..." option disabled - OriginalGriff[/edit]
Updated 30-Mar-11 21:30pm

It's a bad idea to define any objects in header file. With declarations of types or functions it's fine, they can repeat. With the object, you need to declare as extern it in header file and define it separately in some C++ (sometimes people use C files just for this purpose). In this way, you can have multiple declaration but unique definition. If you fail to organize it in this manner, the linker will complain: it needs unique symbol for a global object. By the way, know the difference between declaration and definition.

I would avoid it by using static members of classes. Avoiding any static ("global") declarations is even better. It's always possible to re-design collaboration between your objects.

Share this answer
Olivier Levrey 31-Mar-11 4:24am    
My 5 for this one. Global variables are Dark Side of the Force: very tempting because easier to use for a start, but once you start using them they just make you fall deeper and deeper...
I know what I am talking about since I maintained a program designed by a guy who loved them!
Sergey Alexandrovich Kryukov 31-Mar-11 12:42pm    
Thank you Olivier.
I know exactly what do you mean about maintaining code after someone like that. It's the worse thing.
Sergey Alexandrovich Kryukov 31-Mar-11 13:04pm    
This is great. I like it. So many thing are like Dark Side of the Force!

And by the way, many things are just like the Force. Duck tape, for example -- did you know that? :-)
[no name] 31-Mar-11 4:32am    
Good answer.
Sergey Alexandrovich Kryukov 31-Mar-11 12:43pm    
Thank you, Thaddeus.
You should:

  • Declare such variables as extern in the header file.
  • Define such variables in one source file.

A simple example:

extern int g_count;

//.. (you may optionally include common.h)
int g_count=0;

#include "common.h"
//.. use 'g_count' here

Share this answer
Olivier Levrey 31-Mar-11 4:27am    
My 5. But I would say a word about the danger of using global variables...
CPallini 31-Mar-11 5:35am    
Thank you.
I usually don't give such advices (since often there are a lot of folks already doing it), anyway you're right and please feel free to improve my answer.
Shiva S.S 31-Mar-11 5:55am    
Thank you so much for your excellent answer. Now i realised that, my assumption was wrong, and learned this.
Sergey Alexandrovich Kryukov 31-Mar-11 12:44pm    
Thank you very much for taking labor of showing this thing in code. I wanted to sleep to much to do that back then.
My 5.
if you have multiple variables that need to be accessed by both derived classes, include the variables in the base class itself, you'll have one variable for each class instance, if you need access to the same one, consider making it a static member, this should be a better option than making it global.
Share this answer
I think you header file is including more than once, Though you are not including it because in every derived class is including it implicitly.Add #pragma once at the very beginning of your definition header file to make the header file include once.
Share this answer
Albert Holguin 31-Mar-11 20:35pm    
technically you're correct, so i wouldn't have voted you down... but its not the appropriate solution since globals are considered bad practice, gave you a 4 to offset the markdown...

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900