View Full Version : Visual C++ 2005 Designer
I am using the inbuilt designer to build a Multi Document Interface. Now I can inherit the Children forms form the Parent and display them or access their objects. The designer uses the Header flies for everything.
The Problem occurs when I try to inherit a object form the Parent form to one of the Children.
Example:
(Parent Form/parent.h)
#include "Child.h"
ref class Parent: ......
{
Child^ c = gcnew Child;(No problem here)
bla bla.....
}
But when I also try to do this
(Child Form/child.h)
#include "Parent.h"
ref class Child: ....
{
Parent^ p = gcnew Parent;(give me a bunch of errors that I know are not wrong)
bla bla
}
I can find anything on this on the internet or the help files.
Can some one explain some possible reasons or what it means? Thanks
Can you post the errors you're getting?
Form21.h(47) : error C2143: syntax error : missing ';' before '^'
Form21.h(47) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
Form21.h(47) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
Form21.h(47) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
Form21.h(47) : error C3845: 'test::Form2::form': only static data members can be initialized inside a ref class or value type
I get same exact errors from cpp files too that have the #include "Form1.h".
These are very basic errors that i dont have but appear when i do this particular thing.
I am starting to think what i am trying to do is not possible or is really complicated.
Gamer930
18-12-2006, 17:35
I'm pretty sure what you are trying to do is not possible. . .
Normally how it is a parent can't call methods of a child. I'm unsure why you would want create a parent in a child anyways because the child inherits the parent's methods. . . Depending on what you are planning on doing panels might be the way to go. The MSDN Help is a great tool for any programmer. The help section for Inheritance is at http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vbcon/html/vbconforminheritance.asp
Can you explain why the parent needs to call child functions. I am so confused.
Gamer930
18-12-2006, 17:58
I was just now actually studying for my Software final and saw a section of notes that that will help explain Parent / Child even more. . .
2.3 Inheritance
·Allows the derivation of one class from another.
·Subclasses or child or derived classes contain all of the methods and attributes of the parent or super or base class.
·The subclass may override some of the inherited methods
·The subclass may also add new methods and attributes.
oExample: Military platform base class – robot, UAV, soldier, tank, airplane, etc. child classes.
·Specialization – a subclass that inherits from a superclass and has additional specification. Elevator button is a kind of control but all buttons are not elevator buttons. Elevator button is a specialized button.
·Generalization – the opposite of specialization.
·Inheritance increase reuse and thus reduces the total volume of code needed – less to write, test, document, etc.
I'm pretty sure what you are trying to do is not possible. . .
Normally how it is a parent can't call methods of a child. I'm unsure why you would want create a parent in a child anyways because the child inherits the parent's methods. . . Depending on what you are planning on doing panels might be the way to go. The MSDN Help is a great tool for any programmer. The help section for Inheritance is at http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vbcon/html/vbconforminheritance.asp
I have been pouring through that help, especially the inheritance part. I think I finally get it: I was trying to inherit from a form that had already inherited it. In the MDI forms I was inheriting the children form the parent, then I was trying to inherit the parent form the children again. And this wouldn't work.
I might just have to go with panels. Only think I don't like about them is that hey are not as easy as form windows to arrange and close and open as you wish.
Any other suggestions?
Any other suggestions?
I think what you are trying to do is call a method that is a member of the Parent class from a MDI child form of class Child. If that is not what you are trying to do then disregard the rest of this post, and I'm confused.
If you are trying call a method that is on the MDI parent form, then this has nothing to do with inheritance, other than that both classes must inherit from the System.Windows.Forms.Form class. Note that the Form class has a property called MdiParent (http://msdn2.microsoft.com/en-us/library/system.windows.forms.form.mdiparent.aspx) that is a reference to the instance of the Form that is the MDI container. Now, to access methods that are members of the MDI parent, you have to cast that property as whatever class it really is. From what you posted, it would be:
Parent^ myParent = dynamic_cast<Parent^>(this->MdiParent);
From there you can call methods normally:
myParent->SomeMethod();
You do not use gcnew in this case because you don't want to create a new instance of the Parent class, you just need to use the reference and cast it correctly.
Thanks.
I put this in
Main^ m = dynamic_cast<Main^>(this->MdiParent);
and I got these errors
Form3.h(268) : error C2065: 'Main' : undeclared identifier
Form3.h(268) : error C2065: 'm' : undeclared identifier
Form3.h(268) : error C2061: syntax error : identifier 'Main'
Yeah, I did put the #include "Main.h" above.
:(
esquared
20-12-2006, 17:46
After reading through this (and your post from September about a similar type of issue), it's not readily apparent how your MDI project is set up. My first MDI project in VC++ took a lot of hours of hair pulling and coffee until I could readily pass information around.
If you would like, PM me and you can zip up & email me your project to look at. I'm assuming you've been using the free VC++ available from Microsoft's website?
I am curious to see your project setup as well. You should have a Main.h which houses the Main class declaration, and a Main.cpp for the Main class's code. You should also have a Form3.h and a Form3.cpp. You need to make sure that any code that references the Main class from the Form3 class resides in the Form3.cpp, since you can't have Main.h include Form3.h while at the same time Form3.h includes Main.h since this will cause errors.
I zipped a simple MDI demo application and attached it to the post. I hope it shows what I am talking about.
If you would like, PM me and you can zip up & email me your project to look at. I'm assuming you've been using the free VC++ available from Microsoft's website?
Yea. I will PM once I figure out what I am trying to do.
I am curious to see your project setup as well. You should have a Main.h which houses the Main class declaration, and a Main.cpp for the Main class's code. You should also have a Form3.h and a Form3.cpp. You need to make sure that any code that references the Main class from the Form3 class resides in the Form3.cpp, since you can't have Main.h include Form3.h while at the same time Form3.h includes Main.h since this will cause errors.
So basically you are saying that I should put all my functions in the cpp file. I only need to really do this for the children, right? Currently, my project has everything in the header files. I was trying that but didn't know how to set it for this compiler. Thanks for the sample. This is exactly what I was looking for. When school finally ends tomorrow, I will try it out.
esquared
21-12-2006, 16:55
So basically you are saying that I should put all my functions in the cpp file. I only need to really do this for the children, right? Currently, my project has everything in the header files. I was trying that but didn't know how to set it for this compiler. Thanks for the sample. This is exactly what I was looking for. When school finally ends tomorrow, I will try it out.
The short answer to your question is that function declarations should go into the header files (along with documentation of what the functions do :ahh: ), and the function definitions should go in the cpp files.
The long answer is best left to some research into good programming practices, you can always start with the ubiquitous Wikipedia (http://en.wikipedia.org/wiki/Header_file) to get more information.
-Eric
Thanks for all the help.
Now I have a new problem and I can't find any useful information on it.
Main^ main = dynamic_cast<Main^>(this->MdiParent);
for(count = 0; count < 10; count++)
{
number = main->serialPort1->BytesToRead; //Error
}
An unhandled exception of type 'System.NullReferenceException' occurred in GUI Control Interface.exe
Additional information: Object reference not set to an instance of an object.
This happens when I start sending some serial data form a PIC to the comp.
Searching on the internet hasn't explained it much.
Tom Bottiglieri
30-12-2006, 03:56
T
This happens when I start sending some serial data form a PIC to the comp.
May I ask what type of project you are working on?
May I ask what type of project you are working on?
It's a secret...
No, not really.
To my knowledge there is no real debugging system for the RC other than the printf statements. This program will allow you to watch 10 values(16 bit) at any given time. Each column has a list to choose form. There will be a preset of pwms, analogs, digital I/O, OI port values, etc. But there will also be some extra values, ex. X1, which when programed into the RC allows you watch any variable (16 bit or less).
Ex: x_1 = some_count = .......;
Then you select the X1 in any column and you start seeing a printf style stream of values.
Helpful when you are debugging a whole function. You can assign each variable in your function to a x_1 through x_x and watch all their values simultaneously.
The PIC is simulating an RC. (Don't have access to an RC one right now)
If I can get around to it(probably won't), the program might actually be able to control the robot using this:
(Its not done, not even close...)
Now I have a new problem and I can't find any useful information on it.
Main^ main = dynamic_cast<Main^>(this->MdiParent);
for(count = 0; count < 10; count++)
{
number = main->serialPort1->BytesToRead; //Error
}
Fails with:
An unhandled exception of type 'System.NullReferenceException' occurred in GUI Control Interface.exe
Additional information: Object reference not set to an instance of an object.
This happens when I start sending some serial data form a PIC to the comp.
Searching on the internet hasn't explained it much.
Anyone?
Anyone?
I dont know VC++ but heres my ideas. Do a try catch and see if that give you an error code that means something more.
I dont know VC++ but heres my ideas. Do a try catch and see if that give you an error code that means something more.
I kinda tried this right now, and it didn't help.
Can someone explain what it means when such an exception is thrown?
Thanks
Gamer930
30-12-2006, 15:13
Main^ main = dynamic_cast<Main^>(this->MdiParent);
for(count = 0; count < 10; count++)
{
number = main->serialPort1->BytesToRead; //Error
}
This could be couple things. . . The problem could be line before also. . .
Something isn't initialized and there is a NULL Pointer and you are trying to assign a NULL pointer to a data slot. .
Looking in this instance my guess is there isn't Serial Communications between the PIC and the computer, and that serialPort1 is probably NULL ??
My suggestion would be when you get a problem like this is set a breakpoint couple lines before the error and then run till it gets the breakpoint and do System Watches on the important variables and use the Step Over and Step Into to go step by step through the for loop. . . If something is null it normally comes up as something in red text or a value that you know isn't correct in the watch window.
Bob22341
30-12-2006, 18:46
Your problem comes when "child" calls "parent"
In c++ once you have included a file once, you can't do it again without causing build errors. You create a continual loop with including parent and chile again and again.
If you want to go into inheritance issues and calling parent functions you can, there is an easier way. Create another file called "parenta" and copy over all of the code from "parent" Then slightly change all of the function and class names to avoid build errors, and your almost done.
Change "child" to call "parenta" instead of "parent" and problem solved.
"parent" should include "child" which should include "parenta"
Hopefully that solves your problem :)
Your problem comes when "child" calls "parent"
In c++ once you have included a file once, you can't do it again without causing build errors. You create a continual loop with including parent and chile again and again.
If you want to go into inheritance issues and calling parent functions you can, there is an easier way. Create another file called "parenta" and copy over all of the code from "parent" Then slightly change all of the function and class names to avoid build errors, and your almost done.
Change "child" to call "parenta" instead of "parent" and problem solved.
"parent" should include "child" which should include "parenta"
Hopefully that solves your problem :)
This is not for the null reference exception, is it?
Thanks, anyway, but I think I got the original problem one fixed. The dynamic cast thing seems to be working. This program is already taking much longer than excepted...quite frustrating.
Again, thanks...I really need to get going on this thing.
Ok here is my function in DataView.cpp:
Main^ main = dynamic_cast<Main^>(this->MdiParent);
for(count = 1; count < 11; count++)
{
while(main->serialPort1->BytesToRead < 1);
value = main->serialPort1->ReadByte();
value <<= 8;
value += main->serialPort1->ReadByte();
List_Select(count, value);
} // FAILS right after this if there is line after
main->done_flag = 1; //Here
main->~Main(); //Or here if the above line is commented out
System.NullReferenceException
I don't get it: I am just calling something else form the main after the ReadByte(), but I get an Null exception.
Any Ideas?
Do you guys think this is a worthwhile crusade, in terms of usefulness?
Are there any other options?
If anyone is interested I will release it...Hopefully the main part will be done by then...
esquared
31-12-2006, 12:46
I'm not in front of VC++ right now, but typically there is an Object Browser or that type of thing that allows you to view objects, variables, arrays, pointers, etc when running the program in debugging mode. It might just be part of their debug menu to watch variables, etc. I would watch both the main pointer, and the main MdiParent object itself. You should be able to follow the pointer in the watch menu to see it correctly reference the class's methods, members, etc. Again, since I'm not in front of the IDE I can't give you more specific names to look for.
The idea you've come up with seems pretty useful, the only question will be the overhead on the serial port/quantity of interrupts if your RC is interrupt heavy. It was somewhat difficult to tell from the GUI screen, but how many bytes of data will be sent per "GUI update"? And this will be called every "slow loop" to update or every "fast loop"?
The idea you've come up with seems pretty useful, the only question will be the overhead on the serial port/quantity of interrupts if your RC is interrupt heavy. It was somewhat difficult to tell from the GUI screen, but how many bytes of data will be sent per "GUI update"? And this will be called every "slow loop" to update or every "fast loop"?
I made this to help reduce the load on the RC by not having to do printf statements, where it has to output every character as a byte. It uses a semi-dynamic communication protocol.
The RC has to only read 2 bytes every program loop unless some value has been changed on the GUI. This just to make sure there is a link, otherwise after 20 program loops it will completely stop the robot. If something is touched another 20 bytes have to processed that program loop.
The RC is probably going to be sending about 21 to 50 bytes a program loop depending on your needs. No reprogramming required to change the variables watching. I could lower the bytes send by making it even more dynamic but it will put more processor overhead(Checking which values changed so as to send only that one). So its a choice between more processor overhead or more serial port work; in the end it is about the same load on the processor. If you just want the DataView functionality then the RC only has to send about 21-22 bytes(16 bit values and 1 or 2 starter bytes) per loop to fill all 10 columns.
About the Problem:
It seems to be only happening(returning a null reference) when I call the serialPort1 from the another form
Main^ main = dynamic_cast<Main^>(this->MdiParent);
main->serialPort1->ReadByte();
If I call it in the main form(where it resides) with just, serialPort1->ReadByte(), it working and reads fine.
I have been using the debug features in the Visual Studio and though it has gotten me close, it hasn't exactly helped me pinpoint the source.
Are you still having problems with the SerialPort?
This question has nothing to do with debugging the app, but is this designed to be connected to the RC's serial port or the dashboard port on the OI?
Bob22341
02-01-2007, 12:33
Try adding brackets after your while statement. This is probably your problem because your while statement isn't doing anything right now.
ex:
for(){
while(){
arguments
}
}
p.s. The first dashboard is something like what you are trying to develop. Have you taken a look at it?
esquared
02-01-2007, 12:55
Try adding brackets after your while statement. This is probably your problem because your while statement isn't doing anything right now.
ex:
for(){
while(){
arguments
}
}
That while loop is waiting until there is at least 1 byte received, then it fails the conditional and packs the two bytes into a 16 bit variable. It's just a "smart" waiting function.
Although on further inspection it might be a good idea to ensure the while loop waits to get 2 bytes instead of just 1, since you end up using 2 bytes of data. That way you don't accidentally skip a byte if you ReadByte() twice before the second byte you assume is there hasn't shown up. Thats the fun of events that are asynchronous :)
That while loop is waiting until there is at least 1 byte received, then it fails the conditional and packs the two bytes into a 16 bit variable. It's just a "smart" waiting function.
Although on further inspection it might be a good idea to ensure the while loop waits to get 2 bytes instead of just 1, since you end up using 2 bytes of data. That way you don't accidentally skip a byte if you ReadByte() twice before the second byte you assume is there hasn't shown up. Thats the fun of events that are asynchronous :)
Good point
The problem is it fails when i call in the other forms from the main:
main->...anything!!! (only when the pic is sending some data over the serial port to the computer, otherwise it runs fine. And I think it only happens in this function)
comes up with: Object reference not set to an instance of an object
I will try to get some more info.
This question has nothing to do with debugging the app, but is this designed to be connected to the RC's serial port or the dashboard port on the OI?
It is going to be use full duplex communication on the RC's Program port. The computer is supposed to have full control of the robot, including the hardware(but we will see how far this goes)
The dashboard is quite limiting in that you have reprogram your robot if you want to display different values. This should in theory allow you access any previously programed( and not removed) variables on the fly. The RC doesn't flood the program with all the stuff at the same time(wasting its processor time too). Rather when you change something in any column the Application send the RC which new variables to send to the App.
Thanks everyone for all the help.
I have finally gotten it to perform full duplex communications with a Pic.
All I have to do is select a variable form the drop down menu and it will instantly change show me what the new variable values are in real time. Never thought I would get it to work. Started in summer...got it working a couple days ago...
Thanks again everyone, but I might have more questions...
vBulletin® v3.6.4, Copyright ©2000-2017, Jelsoft Enterprises Ltd.