Overview
Examples
Screenshots
Comparisons
Applications
Download
Documentation
Tutorials
Bazaar
Status & Roadmap
FAQ
Authors & License
Forums
Funding Ultimate++
Search on this site
Search in forums












SourceForge.net Logo
Home » Extra libraries, Code snippets, applications etc. » U++ users applications in progress and useful code snippets, including reference examples! » How to call the methods of a class from another class
How to call the methods of a class from another class [message #2274] Wed, 05 April 2006 17:44 Go to next message
forlano is currently offline  forlano
Messages: 1182
Registered: March 2006
Location: Italy
Senior Contributor
The game is becoming hard. I need some interaction among widgets. The following problem is more general of what may appear.

The problem: in our class App we have a menu, status bar and 3 tabs. In particular the status bar has the InfoCtrl info1 that it is time begin to work (a lazy man can't see other doing nothing Smile ).

I would like that it shows the number of the records added in the arrayctrl. So I did the following (naive) steps:

1. define a method in class App able to modify the info1 ctrl:

     void UpdateInfo(String s)  { info1 = s;}


It works when it is called from inside the class App (by the way, the operator <<= now doesn't work with info1, is it normal?).

2. At the end of the method AddPlayer() I have added the line:
     App.UpdateInfo(" from tab1!");


3. run

Surprised ... the compiler start to cry and complain as I were a criminal. Now that I was enjoing with C++ something happened. Who knows how to let the things work?

Luigi
Re: How to call the methods of a class from another class [message #2278 is a reply to message #2274] Wed, 05 April 2006 18:13 Go to previous messageGo to next message
fudadmin is currently offline  fudadmin
Messages: 1321
Registered: November 2005
Location: Kaunas, Lithuania
Ultimate Contributor
Administrator
forlano wrote on Wed, 05 April 2006 16:44



Surprised ... the compiler start to cry and complain as I were a criminal. Now that I was enjoing with C++ something happened. Who knows how to let the things work?

Luigi


Could you post for lazy people at least a few lines of that cry?
Edit: and/or post small testing apps...

[Updated on: Wed, 05 April 2006 18:15]

Report message to a moderator

Re: How to call the methods of a class from another class [message #2280 is a reply to message #2278] Wed, 05 April 2006 18:49 Go to previous messageGo to next message
forlano is currently offline  forlano
Messages: 1182
Registered: March 2006
Location: Italy
Senior Contributor
edi
fudadmin wrote on Wed, 05 April 2006 18:13


Could you post for lazy people at least a few lines of that cry?
Edit: and/or post small testing apps...


... sorry, here it is the compiler message and editmask.lay (the cpp file is attached. In it look for the line with
App.UpdateInfo(" from tab1!"); )

It seems the compiler do not see some declaration. I've tried to exchange the relative position of the class but with no positive result.

Luigi

====================== compiler =========================
main2.cpp
C:\MyApps\tabs\main2.cpp: In member function `void Tab1::AddPlayer()':
C:\MyApps\tabs\main2.cpp:94: error: `App' undeclared (first use this function)
C:\MyApps\tabs\main2.cpp:94: error: (Each undeclared identifier is reported only once for each function it appears in.)

C:\MyApps\tabs\main2.cpp: At global scope:
C:\MyApps\tabs\main2.cpp:206: error: `struct App' used prior to declaration
C:\MyApps\tabs\main2.cpp:239: error: `App' has not been declared
C:\MyApps\tabs\main2.cpp: In function `void SubSubMenu(Bar&)':
C:\MyApps\tabs\main2.cpp:241: error: invalid use of `this' in non-member function
C:\MyApps\tabs\main2.cpp:241: error: `CLASSNAME' has not been declared
C:\MyApps\tabs\main2.cpp:241: error: `ShowNumber' undeclared (first use this function)
C:\MyApps\tabs\main2.cpp: At global scope:
C:\MyApps\tabs\main2.cpp:245: error: `App' has not been declared
C:\MyApps\tabs\main2.cpp: In function `void FileMenu(Bar&)':
C:\MyApps\tabs\main2.cpp:246: error: invalid use of `this' in non-member function
C:\MyApps\tabs\main2.cpp:246: error: `CLASSNAME' has not been declared
C:\MyApps\tabs\main2.cpp:246: error: `EnableNumbers' undeclared (first use this function)
C:\MyApps\tabs\main2.cpp:246: error: `numbers_enabled' undeclared (first use this function)
C:\MyApps\tabs\main2.cpp:247: error: invalid use of `this' in non-member function
C:\MyApps\tabs\main2.cpp:247: error: `CLASSNAME' has not been declared
C:\MyApps\tabs\main2.cpp:248: error: invalid use of `this' in non-member function
C:\MyApps\tabs\main2.cpp:248: error: `CLASSNAME' has not been declared
C:\MyApps\tabs\main2.cpp:248: error: `Exit' undeclared (first use this function)
C:\MyApps\tabs\main2.cpp: At global scope:
C:\MyApps\tabs\main2.cpp:253: error: `App' has not been declared
C:\MyApps\tabs\main2.cpp: In function `void EditMenu(Bar&)':
C:\MyApps\tabs\main2.cpp:254: error: invalid use of `this' in non-member function
C:\MyApps\tabs\main2.cpp:254: error: `CLASSNAME' has not been declared
C:\MyApps\tabs\main2.cpp:254: error: `TestEdit' undeclared (first use this function)
C:\MyApps\tabs\main2.cpp:255: error: invalid use of `this' in non-member function
C:\MyApps\tabs\main2.cpp:255: error: `CLASSNAME' has not been declared
C:\MyApps\tabs\main2.cpp:255: error: `Dummy' undeclared (first use this function)
C:\MyApps\tabs\main2.cpp:256: error: invalid use of `this' in non-member function
C:\MyApps\tabs\main2.cpp:256: error: `CLASSNAME' has not been declared
C:\MyApps\tabs\main2.cpp:257: error: invalid use of `this' in non-member function
C:\MyApps\tabs\main2.cpp:257: error: `CLASSNAME' has not been declared
C:\MyApps\tabs\main2.cpp: At global scope:
C:\MyApps\tabs\main2.cpp:261: error: `App' has not been declared
C:\MyApps\tabs\main2.cpp: In function `void HelpMenu(Bar&)':
C:\MyApps\tabs\main2.cpp:263: error: invalid use of `this' in non-member function
C:\MyApps\tabs\main2.cpp:263: error: `CLASSNAME' has not been declared
C:\MyApps\tabs\main2.cpp:263: error: `Dummy' undeclared (first use this function)
C:\MyApps\tabs\main2.cpp:264: error: invalid use of `this' in non-member function
C:\MyApps\tabs\main2.cpp:264: error: `CLASSNAME' has not been declared
C:\MyApps\tabs\main2.cpp: At global scope:
C:\MyApps\tabs\main2.cpp:268: error: `App' has not been declared
C:\MyApps\tabs\main2.cpp: In function `void TopMenus(Bar&)':
C:\MyApps\tabs\main2.cpp:269: error: invalid use of `this' in non-member function
C:\MyApps\tabs\main2.cpp:269: error: `CLASSNAME' has not been declared
C:\MyApps\tabs\main2.cpp:270: error: invalid use of `this' in non-member function
C:\MyApps\tabs\main2.cpp:270: error: `CLASSNAME' has not been declared
C:\MyApps\tabs\main2.cpp:271: error: invalid use of `this' in non-member function
C:\MyApps\tabs\main2.cpp:271: error: `CLASSNAME' has not been declared
C:\MyApps\tabs\main2.cpp: At global scope:
C:\MyApps\tabs\main2.cpp:275: error: `App' has not been declared
C:\MyApps\tabs\main2.cpp:276: error: ISO C++ forbids declaration of `App' with no type
C:\MyApps\tabs\main2.cpp: In function `int App()':
C:\MyApps\tabs\main2.cpp:276: error: `int App()' used prior to declaration
C:\MyApps\tabs\main2.cpp:278: error: `numbers_enabled' undeclared (first use this function)
C:\MyApps\tabs\main2.cpp:279: error: `menu' undeclared (first use this function)
C:\MyApps\tabs\main2.cpp:279: error: `AddFrame' undeclared (first use this function)
C:\MyApps\tabs\main2.cpp:280: error: invalid use of `this' in non-member function
C:\MyApps\tabs\main2.cpp:280: error: `CLASSNAME' has not been declared
C:\MyApps\tabs\main2.cpp:283: error: insufficient contextual information to determine type
C:\MyApps\tabs\main2.cpp:286: error: `status' undeclared (first use this function)
C:\MyApps\tabs\main2.cpp:286: error: `info1' undeclared (first use this function)
C:\MyApps\tabs\main2.cpp: In function `void GuiMainFn_()':
C:\MyApps\tabs\main2.cpp:294: error: `Title' has not been declared
C:\MyApps\tabs\main2.cpp:294: error: request for member of non-aggregate type before '(' token
1 file(s) compiled in (0:07.18) 7188 msec/file

=========================== editmask.lay =================

LAYOUT(Tab1Layout, 704, 368)
ITEM(LabelBox, dv___0, SetLabel(t_("Edit Player")).LeftPosZ(16, 684).TopPosZ(16, 80))
ITEM(Label, dv___1, SetLabel(t_("Name")).SetAlign(ALIGN_CENTER).LeftPosZ(28, 132).TopPosZ(32, 19))
ITEM(Label, dv___2, SetLabel(t_("Fed")).SetAlign(ALIGN_CENTER).LeftPosZ(192, 40).TopPosZ(32, 19))
ITEM(Label, dv___3, SetLabel(t_("Birthday")).SetAlign(ALIGN_CENTER).LeftPosZ(236, 56).TopPosZ(32, 19))
ITEM(Label, dv___4, SetLabel(t_("Gender")).SetAlign(ALIGN_CENTER).LeftPosZ(288, 56).TopPosZ(32, 19))
ITEM(Label, dv___5, SetLabel(t_("Title")).SetAlign(ALIGN_CENTER).LeftPosZ(344, 40).TopPosZ(32, 19))
ITEM(Label, dv___6, SetLabel(t_("ID FIDE")).SetAlign(ALIGN_CENTER).LeftPosZ(396, 48).TopPosZ(32, 19))
ITEM(Label, dv___7, SetLabel(t_("RAT FIDE")).SetAlign(ALIGN_CENTER).LeftPosZ(460, 48).TopPosZ(32, 19))
ITEM(Label, dv___8, SetLabel(t_("ID Nat")).SetAlign(ALIGN_CENTER).LeftPosZ(524, 48).TopPosZ(32, 19))
ITEM(Label, dv___9, SetLabel(t_("Rat Nat")).SetAlign(ALIGN_CENTER).LeftPosZ(584, 48).TopPosZ(32, 19))
ITEM(Label, dv___10, SetLabel(t_("K")).SetAlign(ALIGN_CENTER).LeftPosZ(644, 24).TopPosZ(32, 19))
ITEM(EditString, editName, MaxLen(26).LeftPosZ(28, 160).TopPosZ(56, 19))
ITEM(EditString, editCountry, MaxLen(4).NotNull(true).LeftPosZ(196, 32).TopPosZ(56, 19))
ITEM(EditInt, editNatId, Min(0).Max(99999999).NotNull(true).LeftPosZ(516, 60).TopPosZ(56, 19))
ITEM(EditString, editBirth, MaxLen(11).NotNull(true).LeftPosZ(236, 56).TopPosZ(56, 19))
ITEM(EditInt, editFIDERat, Min(0).Max(9999).NotNull(true).LeftPosZ(460, 48).TopPosZ(56, 19))
ITEM(Option, tsex, SetLabel(t_("Male")).LeftPosZ(300, 42).TopPosZ(56, 20))
ITEM(DropList, editTitle, DisplayAll(true).LeftPosZ(348, 42).TopPosZ(56, 20))
ITEM(EditInt, editFIDEId, Min(0).Max(99999999).NotNull(true).LeftPosZ(396, 56).TopPosZ(56, 19))
ITEM(Button, btnModify, SetLabel(t_("Modify Player")).LeftPosZ(152, 96).TopPosZ(116, 24))
ITEM(EditInt, editNatRat, Min(0).Max(9999).NotNull(true).LeftPosZ(584, 44).TopPosZ(56, 19))
ITEM(EditInt, kcoeff, Min(0).Max(99).NotNull(true).LeftPosZ(648, 24).TopPosZ(56, 19))
ITEM(Button, btnClear, SetLabel(t_("Clear Data")).LeftPosZ(280, 96).TopPosZ(116, 24))
ITEM(Button, btnAdd, SetLabel(t_("Add Player")).LeftPosZ(24, 96).TopPosZ(116, 24))
ITEM(ArrayCtrl, arr, Moving(true).Removing(true).AppendLine(true).LeftPosZ(16, 680).TopPosZ(172, 184))
END_LAYOUT

  • Attachment: main2.cpp
    (Size: 8.61KB, Downloaded 1771 times)
Re: How to call the methods of a class from another class [message #2281 is a reply to message #2280] Wed, 05 April 2006 19:00 Go to previous messageGo to next message
fudadmin is currently offline  fudadmin
Messages: 1321
Registered: November 2005
Location: Kaunas, Lithuania
Ultimate Contributor
Administrator
I knew this. You are the criminal! Twisted Evil Especially, if you practice like making dead things to be alive and work for you!

App is not an instance! So it's dead.
Moreover, if it's declared below your call. It's double dead!

And you want this to work?! Smile
Edit: This supposed to be humor Smile

[Updated on: Wed, 05 April 2006 19:13]

Report message to a moderator

Re: How to call the methods of a class from another class [message #2282 is a reply to message #2281] Wed, 05 April 2006 19:07 Go to previous messageGo to next message
fudadmin is currently offline  fudadmin
Messages: 1321
Registered: November 2005
Location: Kaunas, Lithuania
Ultimate Contributor
Administrator
main2.cpp
C:\MyApps\tabs\main2.cpp: In member function `void Tab1::AddPlayer()':
C:\MyApps\tabs\main2.cpp:94: error: `App' undeclared (first use this function)
C:\MyApps\tabs\main2.cpp:94: error: (Each undeclared identifier is reported only once for each function it appears in.)

C:\MyApps\tabs\main2.cpp: At global scope:
C:\MyApps\tabs\main2.cpp:206: error: `struct App' used prior to declaration
C:\MyApps\tabs\main2.cpp:239: error: `App' has not been declared
Re: How to call the methods of a class from another class [message #2283 is a reply to message #2281] Wed, 05 April 2006 19:16 Go to previous messageGo to next message
fudadmin is currently offline  fudadmin
Messages: 1321
Registered: November 2005
Location: Kaunas, Lithuania
Ultimate Contributor
Administrator
And remember you interact with class instances not classes!!!
Re: How to call the methods of a class from another class [message #2287 is a reply to message #2283] Wed, 05 April 2006 21:05 Go to previous messageGo to next message
fudadmin is currently offline  fudadmin
Messages: 1321
Registered: November 2005
Location: Kaunas, Lithuania
Ultimate Contributor
Administrator
You can redeem half of your sinn...
To prevent the complain from compiler of "not declared" you
can use "forward declaration", that is,
class App;

by putting it above your calling class.
But this doesn't help to get the real App object(instance) and reference to it by name because it's encapsulated in main() with GUI_APP...
So, to avoid dealing with dead world you should think about alternatives...
1. Travel up (in terms of controls) with GetParent() up to TopWindow... which is not very nice
2. Travel down (in terms of your code) up to your App and use UpdateInfo from where it is declared...
3. Or think other possibilities...

P.S. I will write soon about 2. but you can try yourself.
Hint: you might need to make some objects more public...
Re: How to call the methods of a class from another class [message #2289 is a reply to message #2283] Wed, 05 April 2006 21:12 Go to previous messageGo to next message
forlano is currently offline  forlano
Messages: 1182
Registered: March 2006
Location: Italy
Senior Contributor
fudadmin wrote on Wed, 05 April 2006 19:16

And remember you interact with class instances not classes!!!


Embarassed Oh my God, how criminal I am.

As 'dead things' are concerned at the end I resolved the problem! Very Happy
It was necessary to move all the class definition in the top of the cpp file and sort them in a good manner (there were 4 classes, and the Murphy law say that the good permutation will be only the last one. In fact it was necessary many trials before to found the good one). When I got the solution I've understood why it was so.
Then I've defined a global class instance

App app;

and pronunciate the magic word

GUI_APP_MAIN
{  
  app.Title("Menu, Tabs and Status -v1").Zoomable().Sizeable().Run();
}


For today it is enough. Tomorrow I hope to do a new mistake not less interesting.

Luigi
Re: How to call the methods of a class from another class [message #2290 is a reply to message #2289] Thu, 06 April 2006 00:38 Go to previous messageGo to next message
fudadmin is currently offline  fudadmin
Messages: 1321
Registered: November 2005
Location: Kaunas, Lithuania
Ultimate Contributor
Administrator
I would also suggest you to think if you really need to display the count of records in the status bar. Maybe it's better to make this count appear somewhere near your ArrayCtrl... or maybe to have a variable in App class...
Re: How to call the methods of a class from another class [message #2291 is a reply to message #2290] Thu, 06 April 2006 10:56 Go to previous message
forlano is currently offline  forlano
Messages: 1182
Registered: March 2006
Location: Italy
Senior Contributor
fudadmin wrote on Thu, 06 April 2006 00:38

I would also suggest you to think if you really need to display the count of records in the status bar. Maybe it's better to make this count appear somewhere near your ArrayCtrl... or maybe to have a variable in App class...


In this case the count need to be seen from all tab pages. So the status bar is the more appropriate.
Any way this case was very useful for me. Even in future I need some little interaction among widget that are in different tabs. I hope this will be not a problem.
Luigi
Previous Topic: "Forlano tabs" - fill EditFields from ArrayCtrl Row...
Next Topic: How to create a modal dialog
Goto Forum:
  


Current Time: Thu Mar 28 10:31:56 CET 2024

Total time taken to generate the page: 0.01024 seconds