Free intercalling of Windows Forms : 3 hrs trial, still failed
Background: Visual Studio 2005, C++ environment, in Windows Forms Application
Objective : To call different forms freely from each forms
Scenario : Form1 calls Form2, Form2 calls Form3, Form3 calls Form4, Form4 calls Form2 again.
Clarification - "call" here means make a new object of another Form class and then ShowDialog(). If the new form is closed, itself is also closed.
1 -> 2 -> 3 -> 4 -> 2
My Method : Form1 includes Form2.h, Form2 includes Form3.h, Form3 includes Form4.h, Form4 includes Form2.h again.
Situation : Compilation error. In such circular calling of forms, Form4 declare an object of Form2 by : Form2^ Form2Obj
And, error is:
Form2 : undeclared identifier
Form2 cannot be recognized!
Is there something should be put into stdafx.h instead? I am really upset about this problem...my C++ project is nearly finished and suddenly there is a big problem...
Any ideas are welcomed, thanks in advance!!
Maybe I'm a bit off, but I believe you can fix such a dependency issue by using forward declarations. That means that you don't include Form2.h in Form4.cpp, but just declare Form2 by doing a
at the beginning of Form4.cpp.
class Form2;
at the beginning of Form4.cpp.
Quote:Original post by jkuhlmann
Maybe I'm a bit off, but I believe you can fix such a dependency issue by using forward declarations. That means that you don't include Form2.h in Form4.cpp, but just declare Form2 by doing aclass Form2;
at the beginning of Form4.cpp.
at the beginning of .cpp, not .h, right? I am going to try that.
BUT there is a problem: how about the very original form when the project is created - the Form1? Where is the Form1.cpp? I can't see it in the solution explorer...
Now I "google" for about dozens of websites and I am starting to find that I am getting into a problem of circular dependency of Windows Form which is prohibited by VS2005 and there is YET a solution the problem.
I am now going to make 5 forms(1 call 2, 2call 3, 3 call 4, 4 call a copy of 2)......am I going to do something stupid?
I am now going to make 5 forms(1 call 2, 2call 3, 3 call 4, 4 call a copy of 2)......am I going to do something stupid?
You should have include guards (#pragma once) so the headers wont be included more than once. This then means that Form4 will have no knowledge of a form2, so you put
in front of the class declaration in Form4.h As long as you do not access Form2 in the header file of Form4 then everything should be fine. (You can access Form2 in the Form4 implementation file - .cpp)
class Form2;
in front of the class declaration in Form4.h As long as you do not access Form2 in the header file of Form4 then everything should be fine. (You can access Form2 in the Form4 implementation file - .cpp)
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement