-
April 29th, 2009, 07:25 PM
#1
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?
-
April 29th, 2009, 09:21 PM
#2
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.
-
April 30th, 2009, 06:53 AM
#3
Re: How to share variables/functions between classes from different files
Originally Posted by Peter_APIIT
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;
}
-
April 30th, 2009, 09:27 AM
#4
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...
-
April 30th, 2009, 09:35 AM
#5
Re: How to share variables/functions between classes from different files
in CStation.h do you forward dec. CRemote?
-
April 30th, 2009, 10:44 AM
#6
Re: How to share variables/functions between classes from different files
Originally Posted by Amleto
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.
-
April 30th, 2009, 10:57 AM
#7
Re: How to share variables/functions between classes from different files
Originally Posted by VladimirF
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).
-
April 30th, 2009, 02:13 PM
#8
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|