Forward Linking and Circular Dependency problem

G

Guest

Hi all,

I have just started working on VC++ .Net and have problem with circular
dependencies of 2 files.

I created 2 forms "Form1" and "Form2", which respectively created 2 .h
files, Form1.h and Form2.h and so as 2 classes, "Form1" and "Form2".

Now I want to use object of Form1 inside Form2 and object of Form2 in Form1.

Reason:

1) I want to open Form2 from Form1 when a button is clicked
2) Want to access some public variable in Form1 from Form2.

I all the time get compiling error stating that Form1 is not declared even
if I have included "form1.h" and "form2.h" in "form2.h" and "form1.h",
respectively.

May I know how to solve this problem.

Your help will be appreciated.

Thanks
 
S

Steve McLellan

Hi,

You need to do something like:

// In Form1.h
#include "Form2.h"

__gc class Form1
{
public:
void GetSomethingFromForm2() { .... }
};

// In Form2.h
__gc class Form1; // Don't define or include it.

__gc class Form2
{
void GetSomethingFromForm1(); // Don't implement here
};

// In Form2.cpp
#include "Form1.h"

Form2::GetSomethingFromForm1()
{
// Implement
}

This way you'll forward declare Form1 in Form2, but you won't get a circular
dependency.

HTH,

Steve
 

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