Main Form, Modeless Form, Forward Declaration Problem?

G

Guest

Hello, I'm trying to accomplish the impossible by trying to do something equivalent of this example found here
http://www.c-sharpcorner.com/Code/2003/Dec/DialogTutorial.as

Starting with "Listing 2 - Changing the constructor of the modeless dialog to accept the parent object:" line and go onwards is what I'm trying to do

Basically, there is the main form, and then there's the modeless form. I want the modeless form act like a modal form, except that the modeless form won't block the main form, which is why I dislike modal form. The problem is that modeless form won't report the dialog result, so I came to a webpage link above. Read the example, tried to do it, got stuck

I've read up about forward declaration, but that didn't help. It still kept saying that the main form's class is undefined (C2027 error from compiler). Maybe I'm doing it wrong somehow. This is how it looks like in my current project (cutting out all the uncessary clutter)

main.
===
namespace bla

#include "modelessform.h
public __gc class main : public System::Windows::Forms::For

public: void launchform(

modelessform* box = new modelessform(this)

void showsomething(modelessform* box

// get text from modelessform and put it into main's textBo
statusBar->Text = box->gettext()




modelessform.
=========
namespace bla

public __gc class main; // forward declaratio
public __gc class modelessform : public System::Windows::Forms::For

// created another constructor..
public: modelessform(main* parent

InitializeComponent()
theparentform = parent

String* gettext(){return textBox->Text;
private: main* theparentform

// somewhere very down below..
private: System::Void buttonok_Click(System::Object * sender, System::EventArgs * e

parentform->gettext(); // Compiler spits out C2027 here, saying use of undefined type 'blah::main
Close()




Given that everything compiles and runs fine, no errors/warnings, windows form pops up fine, but when trying the nifty sending parent object trick like the web page does it in C#, I get that annoying C2027. Maybe I'm missing something here or there, or have a misunderstanding that this can be done in C++ but can't. I would think a forward declaration would fix the problem, but that only fixes half the problem. Including the forward declaration of class main in class modeless form fixed the compiler error that main* wasn't define. Moving on, I then try to make theparentform pointer access its own function, and then boom, C2027 error

Help?
 
G

Guest

Looks like I'll just move development to Visual C# to make things less complicating. Code syntax between C# and C++ are miniscule, so C#, here I come!
 
S

Steve McLellan

You've included modelessform.h inside the namespace declaration. I'm not
sure if that's causing you're problem, but it probably won't help.

Somewhere in modelessform you need to include main.h, otherwise, as the
compiler points out, parentform is of an undefined type (it's just a
declaration). If I were you, to avoid the inevitable circular include, I'd
create modelessform.cpp and stick the offending code in there.

HTH,

Steve

Legendary Pansy said:
Hello, I'm trying to accomplish the impossible by trying to do something
equivalent of this example found here:
http://www.c-sharpcorner.com/Code/2003/Dec/DialogTutorial.asp

Starting with "Listing 2 - Changing the constructor of the modeless dialog
to accept the parent object:" line and go onwards is what I'm trying to do.
Basically, there is the main form, and then there's the modeless form. I
want the modeless form act like a modal form, except that the modeless form
won't block the main form, which is why I dislike modal form. The problem is
that modeless form won't report the dialog result, so I came to a webpage
link above. Read the example, tried to do it, got stuck.
I've read up about forward declaration, but that didn't help. It still
kept saying that the main form's class is undefined (C2027 error from
compiler). Maybe I'm doing it wrong somehow. This is how it looks like in my
current project (cutting out all the uncessary clutter):
main.h
====
namespace blah
{
#include "modelessform.h"
public __gc class main : public System::Windows::Forms::Form
{
public: void launchform()
{
modelessform* box = new modelessform(this);
}
void showsomething(modelessform* box)
{
// get text from modelessform and put it into main's textBox
statusBar->Text = box->gettext();
}
}
}

modelessform.h
==========
namespace blah
{
public __gc class main; // forward declaration
public __gc class modelessform : public System::Windows::Forms::Form
{
// created another constructor...
public: modelessform(main* parent)
{
InitializeComponent();
theparentform = parent;
}
String* gettext(){return textBox->Text;}
private: main* theparentform;

// somewhere very down below...
private: System::Void buttonok_Click(System::Object * sender, System::EventArgs * e)
{
parentform->gettext(); // Compiler spits out C2027 here, saying
use of undefined type 'blah::main'
Close();
}
}
}

Given that everything compiles and runs fine, no errors/warnings, windows
form pops up fine, but when trying the nifty sending parent object trick
like the web page does it in C#, I get that annoying C2027. Maybe I'm
missing something here or there, or have a misunderstanding that this can be
done in C++ but can't. I would think a forward declaration would fix the
problem, but that only fixes half the problem. Including the forward
declaration of class main in class modeless form fixed the compiler error
that main* wasn't define. Moving on, I then try to make theparentform
pointer access its own function, and then boom, C2027 error.
 

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