Simple question about Class and Variables
Hi ,
I have written a class "FileSet " in program's Document class.
This class contains other Four classes as "Machine1" , "Machine2",
"Machine 3" and "Machine 4".
Now my problem is Machine 1 can not access variables from Document class or other variables from FileSet.
How to acess them ? Pl help ..
Thanking you ..
Re: Simple question about Class and Variables
Writing a class is like drawing a blueprint. Defining a variable is like building by the blueprint. So, to be able to access an object you must "build" the object but not just keep its blueprint in your desk drawer.
Re: Simple question about Class and Variables
To provide further guidance, it would be helpful if you would post the code for your classes.
Re: Simple question about Class and Variables
Thank u Igor Vartanov sir and 2kaud sir.
Here is the code
Filename : mts_ex4_2cardDoc.h
Filename : mts_ex4_2cardstuff.h
class CMachineSet : public CDocument {
DECLARE_SERIAL(CLineSet)
public:
void Copy(CMachineSet& src);
CMachineSet();
~CMachineSet();
CTestConfig testconfig;
FILE * ResFile;
int x,y;
int DataReadyFlag;
void SaveData();
void ClearDisplay();
void DrawPie(CDC *);
int ReadData();
virtual void Serialize(CArchive& ar);
protected:
// {{AFX_MSG(Cmts_ex4_2cardDoc)
};
The problem is I can acess variables from machineset and fileset from document . But I am
not able to write the X , Y values of MachineSet Data to SaveMachineCord Array from document ,
from a function written in machine set (SaveData)
There are similar many problems with other varibles. Cant Acess Document functions and variables from Machine Set.
PL excuse as I dont know how to use code tags.
Thanking you...
Re: Simple question about Class and Variables
To use code tags - Go Advanced, select the code and then click '#'. The code should be properly formatted before being posted. You should be able to edit your post for this.
Re: Simple question about Class and Variables
Quote:
Originally Posted by
new_2012
The problem is I can acess variables from machineset and fileset from document . But I am
not able to write the X , Y values of MachineSet Data to SaveMachineCord Array from document ,
from a function written in machine set (SaveData)
You were asked to provide a code. This your description does not seem like a code where you experience a problem, you know. :)
Besides, what does that "document" mean? Is it that you have a CDocument* pointer but trying to access CMachineSet members?
Re: Simple question about Class and Variables
Igor Sir , Document means CDocument class from SDI MFC application.
When I try to access variables from CDocument class , it gives error "undeclared identifier ". I dont wish to keep a Global Document Pointer.
Another method would be shift the code to CDocument File , but I am
not happy to write SaveData() which is Machine Set's function into CDocument class. So I am confused.
PL Guide
Re: Simple question about Class and Variables
You have some design issues. If MachineSet is derived from CDocument, then by definition, MachineSet is a CDocument. CDocument already has virtual functions that handle saving data. You should provide overrides of those in your MachineSet class. If that's not appropriate for you, you should evaluate whether MachineSet really should be derived from CDocument.
Re: Simple question about Class and Variables
Quote:
Originally Posted by
new_2012
When I try to access variables from CDocument class , it gives error "undeclared identifier ".
This indicates you do something really unusual for C++. Need to see your code to advise on how to fix.
Quote:
I dont wish to keep a Global Document Pointer.
I have to admit I don't get you here. Whatever "Global Document Pointer" would be, you either follow MFC document/view way, or get rid of MFC document entirely.
Quote:
Another method would be shift the code to CDocument File , but I am
not happy to write SaveData() which is Machine Set's function into CDocument class. So I am confused.
Me either. You cannot/mustn't "shift the code to CDocument", as the class is a part of MFC framework. To modify MFC code you must be qualified enough, which qualification you evidently lack for, and have a really, really good reason to do that.
So, show us your code, how you save data and how you access CDocument members.
Re: Simple question about Class and Variables
Thank You Igor Sir and GCDEF Sir . I got my design mistake. Instead of deriving a class from CDocument
I put a Instance of that class in CDocument and lost access to variables of CDocument. Now I will restructure my code and will make use of Virtual Functions. Thanks a lot.