forward declaration

S

Sergey Muschin

Hi there,

how to handle a forward declaration in managed VC ?

for example, let say i'm working with to form classes form1 and form2.
Now, how can i call a public method of form2 from form1?
I will include form2.h file in form1. Right?

But if i need to call a public method of form1 from form2 now? What do i do?

Regards,
Sergey Muschin
 
C

Carl Daniel [VC++ MVP]

Sergey said:
Hi there,

how to handle a forward declaration in managed VC ?

The same way you would in unmanged C++.
for example, let say i'm working with to form classes form1 and form2.
Now, how can i call a public method of form2 from form1?
I will include form2.h file in form1. Right?

But if i need to call a public method of form1 from form2 now? What
do i do?

Move the implementation of the member in form2 that needs to reference form1
out of the header file and into form2.cpp

// form1.h

#include "form2.h"

ref class form1 {

public:
void foo(form2^ f) {
// do stuff with form 2
}
};

// form2.h

ref class form1;

ref class form2 {
public:
void bar(form1^ f);
};

// form2.cpp

#include "form1.h"

void form1::bar(form1^ f) {
// do stuff with form 1
}

Generally co-dependence between classes like this is indicative of a
non-optimal design. Think about how you can change the composition of your
application to avoid this kind of pattern - it's usually possible,
especially between high level UI concepts like forms.

-cd
 
S

Sergey Muschin

I've been there, tried that.... :(

here is the error message i'm getting

c:\src\msvs8beta2\c++\newkiosk\mainapp\mainapp\frmImaging.h(66) : error
C2512:
'MainApp::frmMain' : no appropriate default constructor available

and the source code is :

ref class frmMain;

public ref class frmImaging : public System::Windows::Forms::Form {

public:
frmImaging(void) {
InitializeComponent();
}

private:
System::Void frmImaging_Shown(System::Object^ sender, System::EventArgs^
e) {
frmMain^ o = gcnew frmMain();
}

};//EOF Class


//////////////////////////////////////////////////
 
C

Carl Daniel [VC++ MVP]

Sergey said:
I've been there, tried that.... :(

here is the error message i'm getting

c:\src\msvs8beta2\c++\newkiosk\mainapp\mainapp\frmImaging.h(66) :
error C2512:
'MainApp::frmMain' : no appropriate default constructor available

and the source code is :

ref class frmMain;

public ref class frmImaging : public System::Windows::Forms::Form {

public:
frmImaging(void) {
InitializeComponent();
}

private:
System::Void frmImaging_Shown(System::Object^ sender,
System::EventArgs^ e) {
frmMain^ o = gcnew frmMain();
}

};//EOF Class

You haven't correctly implemented the pattern I posted. Look at it again
(below) - you need to move the code in frmImaging that requires a complete
definition of frmMain into the .cpp file and out of the header file. In the
..cpp file you can #include "frmMain.h'
//////////////////////////////////////////////////
-cd
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top