Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: VC8.0 MFC
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.
 
Shiva.
 
[edit]Code blocks, "Ignore HTML..." option disabled - OriginalGriff[/edit]
Posted 30-Mar-11 22:21pm
Edited 30-Mar-11 22:30pm
v2
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

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.
 
—SA
  Permalink  
Comments
Olivier Levrey at 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!
SAKryukov at 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.
--SA
SAKryukov at 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? :-)
--SA
Thaddeus Jones at 31-Mar-11 4:32am
   
Good answer.
SAKryukov at 31-Mar-11 12:43pm
   
Thank you, Thaddeus.
--SA
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

You should:


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





A simple example:

(common.h)
//..
extern int g_count;
//..


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


(other.cpp)
#include "common.h"
//.. use 'g_count' here




  Permalink  
v6
Comments
Olivier Levrey at 31-Mar-11 4:27am
   
My 5. But I would say a word about the danger of using global variables...
CPallini at 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.
Siva_Si at 31-Mar-11 5:55am
   
Thank you so much for your excellent answer. Now i realised that, my assumption was wrong, and learned this.
SAKryukov at 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.
--SA
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 4

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.
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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.
  Permalink  
v2
Comments
Albert Holguin at 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)



Advertise | Privacy | Mobile
Web02 | 2.8.141220.1 | Last Updated 31 Mar 2011
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100