473,397 Members | 2,077 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,397 software developers and data experts.

forward declaration

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
Nov 17 '05 #1
6 1322
Are they in the same namespace?
-M

"Sergey Muschin" <se************@tonservices.com> wrote in message
news:Ou****************@TK2MSFTNGP15.phx.gbl...
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

Nov 17 '05 #2
Yes, they are in the same namespace.
"Micus" <No**@nowhere.com> wrote in message
news:%2****************@tk2msftngp13.phx.gbl...
Are they in the same namespace?
-M

Nov 17 '05 #3
Sergey Muschin wrote:
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
Nov 17 '05 #4
I've been there, tried that.... :(

here is the error message i'm getting

c:\src\msvs8beta2\c++\newkiosk\mainapp\mainapp\frm Imaging.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
//////////////////////////////////////////////////
// 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

Nov 17 '05 #5
Sergey Muschin wrote:
I've been there, tried that.... :(

here is the error message i'm getting

c:\src\msvs8beta2\c++\newkiosk\mainapp\mainapp\frm Imaging.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'


//////////////////////////////////////////////////
// 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
Nov 17 '05 #6
Thank You! it's working now!
Nov 17 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: mjm | last post by:
Folks, Please help me with the following problems: ******************************************** 1. I have a class template template<class Base> class Matrix : public Base { /* .... */ }
6
by: Steven T. Hatton | last post by:
Should I be able to forward declare something from a namespace different from the current one? For example the following code compiles: //testdriver.hpp #ifndef TESTDRIVER_HPP #define...
6
by: Markus Dehmann | last post by:
I have a circular dependency between two classes. The FAQ hint about a forward declaration does not help in my case ( How can I create two classes that both know about each other?) Error:...
2
by: Plok Plokowitsch | last post by:
After over a decade of programming in C++ I seem to have missed some substantial point (mental note: am I getting too old for this?). A little bit of help would be *very* appreciated. I'm trying...
2
by: blueblueblue2005 | last post by:
Hi, there was a post several days ago about using forward class declaration to solve the circular including issue, today, one question suddenly came into mind: which class should the forward class...
3
by: Michael Sgier | last post by:
Hello with the original code below I get the error: "forward declaration of `struct CPlayer'" class CPlayer; // what does this do? Instantiate the class CPlayer? // what's a forward...
23
by: mark.moore | last post by:
I know this has been asked before, but I just can't find the answer in the sea of hits... How do you forward declare a class that is *not* paramaterized, but is based on a template class? ...
6
by: Hunk | last post by:
Hi I have a question on usage of forward declarations of templates. While searching through this group , people suggested using forward declarations and typedefs for templates as // in...
4
by: Steve | last post by:
Hi, I always though to return an instance of a class by value, it had to be defined - i.e. forward declaration isn't good enough? Consider the following code snippet: class RGBA; class...
7
by: RedLars | last post by:
Need some help with a couple of questions. Previously I have seen this declaration; struct Student { int age; char name; }; and this definition of an object; struct Student stud;
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.