CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 10 of 10
  1. #1
    Join Date
    Jan 2012
    Location
    India
    Posts
    193

    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 ..

  2. #2
    Join Date
    Nov 2000
    Location
    Voronezh, Russia
    Posts
    6,620

    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.
    Last edited by Igor Vartanov; July 31st, 2013 at 02:06 AM.
    Best regards,
    Igor

  3. #3
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,822

    Re: Simple question about Class and Variables

    To provide further guidance, it would be helpful if you would post the code for your classes.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  4. #4
    Join Date
    Jan 2012
    Location
    India
    Posts
    193

    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...
    Last edited by new_2012; July 31st, 2013 at 04:33 AM. Reason: for code tags

  5. #5
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,822

    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.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  6. #6
    Join Date
    Nov 2000
    Location
    Voronezh, Russia
    Posts
    6,620

    Re: Simple question about Class and Variables

    Quote Originally Posted by new_2012 View Post
    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?
    Last edited by Igor Vartanov; July 31st, 2013 at 05:47 AM.
    Best regards,
    Igor

  7. #7
    Join Date
    Jan 2012
    Location
    India
    Posts
    193

    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

  8. #8
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,635

    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.

  9. #9
    Join Date
    Nov 2000
    Location
    Voronezh, Russia
    Posts
    6,620

    Re: Simple question about Class and Variables

    Quote Originally Posted by new_2012 View Post
    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.

    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.

    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.
    Best regards,
    Igor

  10. #10
    Join Date
    Jan 2012
    Location
    India
    Posts
    193

    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.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured