Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

NewBie question re. Class Access

Status
Not open for further replies.

BeerFizz

Technical User
Jan 23, 2004
28
Hi,

Sorry for the length post, but I thought I should include as much info as possible.
I am just starting to use MS Visual C++ 6.0 and OOP and I’m having a problem passing/getting the handle to one class from another. I have written a simple Dialog Class test program to show my issue.


Test2App
|
|
creates 2 dialogs
Tab1 and Tab2
Tab1 attempts to access a public member in another class “test”



Portions of the code are as follows

Test2App has the following code which creates a property sheet with two tab pages:

CPropertySheet dlgPropertySheet;
DialogTab1 Tab1Page;
Dialog2Tab2 Tab2Page;
dlgPropertySheet.AddPage(&Tab1Page);
dlgPropertySheet.AddPage(&Tab2Page);

Tab1Page.m_Tab1Edit1 = "This is a test";
Tab1Page.m_CheckBoxOne = TRUE;

if (dlgPropertySheet.DoModal() == IDOK)
{
str1 = Tab1Page.m_Tab1Edit1;
str2 = Tab2Page.m_Tab2Edit1;
Tab1Page.SetModified();
}



Dialog Tab1 has the following code:

BOOL DialogTab1::OnInitDialog()
{
CPropertyPage::OnInitDialog();

// TODO: Add extra initialization here

int n = TestRef.AddTwoNum( 5, 8);

return TRUE; // return TRUE unless you set the focus to a control
// EXCEPTION: OCX Property Pages should return FALSE
}



Class Test has the following code:

class Test : public CDialog
{
// Construction
public:
Test(CWnd* pParent = NULL); // standard constructor

int AddTwoNum(int n, int m) {
return n+m;
}



To allow DialogTab1 to access the public member AddTwoNum, I tried to pass the handle of test. The following are excerpts from the code: (hopefully I’m being sufficiently clear?)


In test .h:

#include "DialogTab1.h"

In test.cpp:


Test::Test(CWnd* pParent /*=NULL*/)
: CDialog(Test::IDD, pParent),
DialogTab1( *this)
{
//{{AFX_DATA_INIT(Test)
// NOTE: the ClassWizard will add member initialization here
//}}AFX_DATA_INIT
}

In DialogTab1.h:


class Test; // Forward Declaration

/////////////////////////////////////////////////////////////////////////////
// DialogTab1 dialog

class DialogTab1 : public CPropertyPage
{
DECLARE_DYNCREATE(DialogTab1)

// Construction
public:
DialogTab1(Test &);
~DialogTab1();


private:
Test &TestRef;



In DialogTab1.cpp:


/////////////////////////////////////////////////////////////////////////////
// DialogTab1 property page

IMPLEMENT_DYNCREATE(DialogTab1, CPropertyPage)



DialogTab1::DialogTab1(Test &TestHandle) : CPropertyPage(DialogTab1::IDD),
TestRef( TestHandle)
{
//{{AFX_DATA_INIT(DialogTab1)
m_Tab1Edit1 = _T("");
m_CheckBoxOne = FALSE;
//}}AFX_DATA_INIT
}




DialogTab1::~DialogTab1()
{


I get the following compiler error:

Compiling...
DialogTab1.cpp
C:\Awork\Data Reply\Sample C\test2\DialogTab1.cpp(19) : error C2512: 'DialogTab1' : no appropriate default constructor available

(this is against this code:
/////////////////////////////////////////////////////////////////////////////
// DialogTab1 property page

IMPLEMENT_DYNCREATE(DialogTab1, CPropertyPage) <<<<<<< error on this line



DialogTab1::DialogTab1(Test &TestHandle) : CPropertyPage(DialogTab1::IDD),
TestRef( TestHandle)
{
)

Test.cpp
C:\Awork\Data Reply\Sample C\Test2\Test.cpp(20) : warning C4355: 'this' : used in base member initializer list

(This is against this code:

Test::Test(CWnd* pParent /*=NULL*/)
: CDialog(Test::IDD, pParent),
DialogTab1( *this) <<<<<<<<< error on this line
{
)
C:\Awork\Data Reply\Sample C\Test2\Test.cpp(21) : error C2614: 'Test' : illegal member initialization: 'DialogTab1' is not a base or member
(This is against this code:

Test::Test(CWnd* pParent /*=NULL*/)
: CDialog(Test::IDD, pParent),
DialogTab1( *this)
{ <<<<<<<<< error on this line

)

Generating Code...
Compiling...
test2Dlg.cpp
C:\Awork\Data Reply\Sample C\test2\test2Dlg.cpp(126) : error C2512: 'DialogTab1' : no appropriate default constructor available
Generating Code...



If this is not sufficient to go on, please pm me and I’ll send you a zip of the files.
If anyone can help, I would appreciate it.

Thanks

 
As I can see you need some explanation about the relationship between classes.
If a class M IS AS another class B then you have to inherit M from B.
If a class M HAS A member of class N then the class M declaration should contains a member of type N.
If a class M USE a class N then in the class M there is a member function that takes as argument an object of type N or return an object or a reference of N type.
Of course you can have utility or helper classes in your project and they could not be related with other classes of your project but you need their functionality.
Let be class Test : public CDialog as the utility class.
When you want to call a function of this class like int AddTwoNum() in any other classes you have to instantiate the Test class and call its member like follows:
int MyAnotherClass::Add(int a, int b)
{
Test dlg;
return dlg.AddTwoNum(a,b);
}

Because your project contains GUI there is another way to get access from one dialog to another if their instances exist at run time:
class Test :: public CDialog
{

}

int MyAnotherClass::Add(int a,int b)
{
Test *p1 = (Test *)GetDlgItem(IDD_DIALOG_TEST);
if (p1)
return p1->AddTwoNum(a,b);
esle return MIN_VALUE;

}
-obislavu-
 
obislavu,

thank you very much for your response.

But please consider the following:

Suppose the test class contains private variable which is set by “SetVar” and fetched by “GetVar”.

int MyAnotherClass1::Add(int a, int b)
{
Test dlg;
dlg.SetVar(5);
return;
}

int MyAnotherClass2::Add(int a, int b)
{
Test dlg;
dlg.SetVar(7);
return;
}

int MyAnotherClass3::Add(int a, int b)
{
Test dlg;
int I = dlg.GetVar();
return;
}

If I do this, there are three instantiations of test and who knows what getvar will fetch.

I need to be able to instantiate the test class at a higher level and pass the handle or in some alternative way make the handle available to others.





 
In this case you have to instantiate only one time at the higher place and have a pointer to this instance in MyAnotherClass1 and MyAnotherClass2.
-obislavu-
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top