CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8
  1. #1
    Join Date
    Apr 2009
    Posts
    30

    How to share variables/functions between classes from different files

    I want to share variables/functions between classes. The classes are defined in different .h files. I read that the friend function allows us to do that but I keep getting error messages saying either that the class which holds the data to be shared is not defined or that the function being shared is not a member of the class. From the books and on the internet, the examples are written for classes defined within the same file. Is there a good example on how to do it when the classes are defined in different .h files?

  2. #2
    Join Date
    Apr 2007
    Location
    Mars NASA Station
    Posts
    1,436

    Re: How to share variables/functions between classes from different files

    Show us the code and we can diagnose it together with you.

    Does the variable/function located in a class which required by another class in different .h header file ?

    I just you need to look for external linking.( if i not mistaken)
    Thanks for your help.

  3. #3
    Join Date
    Apr 2009
    Posts
    30

    Re: How to share variables/functions between classes from different files

    Quote Originally Posted by Peter_APIIT View Post
    Show us the code and we can diagnose it together with you.

    Does the variable/function located in a class which required by another class in different .h header file ?

    I just you need to look for external linking.( if i not mistaken)

    Thanks for the reply. As the program is too complex, I enclose the key parts only. Please note that the program was written in Borland C++ Builder 6.


    In CStation.h
    -----------------

    class CStation: public Cbase
    {
    friend class CRemote; // to give class CRemote full access to CStation
    private:
    DoubleArray * data_to_be_shared; // DoubleArray is a class that stores an array of double.
    // I want CRemote to be able to access data_to_be_shared.
    };


    In CStation.cpp
    --------------------
    I have a function to do some computations and stores the values into data_to_be_shared. I want CRemote to access this private data.


    In CRemote.h
    ------------
    class CStation; // forward declaration
    class CRemote : public Cbase
    {
    // codes removed for clarify.
    };


    In CRemote.cpp
    --------

    void CRemote::ProcessData(void)
    {
    CStation station_class; // I got an error saying that CStation is an undefined structure.
    // When I put #include "CStation.h" in CRemote.h and assigned it to
    // data (see below), this error message disappeared but I got an
    // error saying that station_class.data_to_be_shared cannot be
    // accessed.
    DoubleArray *data;

    data = station_class.data_to_be_shared;
    }

  4. #4
    Join Date
    Aug 2000
    Location
    New York, NY, USA
    Posts
    5,656

    Re: How to share variables/functions between classes from different files

    Did you also get an error:
    Code:
    error C2039: 'ProcessData' : is not a member of 'CRemote'
    ?
    Vlad - MS MVP [2007 - 2012] - www.FeinSoftware.com
    Convenience and productivity tools for Microsoft Visual Studio:
    FeinWindows - replacement windows manager for Visual Studio, and more...

  5. #5
    Join Date
    Apr 2008
    Posts
    725

    Re: How to share variables/functions between classes from different files

    in CStation.h do you forward dec. CRemote?

  6. #6
    Join Date
    Apr 2009
    Posts
    30

    Re: How to share variables/functions between classes from different files

    Quote Originally Posted by Amleto View Post
    in CStation.h do you forward dec. CRemote?
    No. Do you mean I have to put:

    class CRemote;

    at the beginning of CStation.h? I just tried and got four error messages (please note that the wordings are not exact as I translated from Japanese into English):

    E2450 undeclared (undefined?) structure 'CStation'
    E2449 station_class's size is unknown or zero
    E2450 undefined structure 'CStation'
    E2315 since there is no type declaration, data_to_be_shared is not a member of CStation.

  7. #7
    Join Date
    Apr 2009
    Posts
    30

    Re: How to share variables/functions between classes from different files

    Quote Originally Posted by VladimirF View Post
    Did you also get an error:
    Code:
    error C2039: 'ProcessData' : is not a member of 'CRemote'
    ?
    No. I declared it under public in the class definition of CRemote (inside the .h file).

  8. #8
    Join Date
    Aug 2000
    Location
    New York, NY, USA
    Posts
    5,656

    Re: How to share variables/functions between classes from different files

    Could you create a small project with only those classes that demonstrate your problem and post it here?
    Now we have to guess what is declared where and how...
    Vlad - MS MVP [2007 - 2012] - www.FeinSoftware.com
    Convenience and productivity tools for Microsoft Visual Studio:
    FeinWindows - replacement windows manager for Visual Studio, and more...

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