GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 28-Sep-2006, 08:47
Alloishus Alloishus is offline
New Member
 
Join Date: Sep 2006
Posts: 15
Alloishus is on a distinguished road

Windows C++ Problem with Class and Functions


OK, a little background first:

I am creating a Windows program using C++ (unmanaged).
I am not new to programming, but I am not very experienced.

I have an issue that I am having trouble with, and it might be a design issue.

Let me try to explain the issue first, without any real code. Because I do not think code is the issue, as much as design and the concept. I know this breaks rule 1, but the code is very trivial.

If anyone has programmed in C++ Windows, then you know that alot of functions are loop functions that terminate when certain messages are sent (WM_CLOSE, WM_DESTROY, etc). The rest of the time, the function is in a loop to access mouse, paint, keyboard, and timer messages.

Ok, so I have a program that has some child windows, each in a file : Child1File.cpp and Child2File.cpp

I also have a class declaration in File3.h (Let's call it CHICKEN). CHICKEN functions are defined in a File3.cpp.

In Child1File.cpp, I include File3.h and instantiate an instance of chicken, called egg ( CHICKEN egg ;).

I ask for some user input to name the egg, which is a property of CHICKEN..... like so egg.szName = TEXT ("Egghead").

I need to display this property in Child2File.cpp.

Now, the problem from what I can gather is that I cannot pass a reference to the egg into the Child2File.cpp function because that is already defined by <windows.h>. (You know HWND, UINT, LPARAM, WPARAM).

I obviously cannot instantiate the object in Child2file.cpp because this would create another instance of Chicken, called egg (totally separate from egg instantiated in Child1File.cpp).

I could instantiate the egg in any file, but since I cannot pass the information to the function like normal, I am in a bind.

So.......would someone be kind enough to show me the error of my reasoning, and a possible solution? Really, if you could just point me to a concept I might read about which would solve this, I would be most certainly grateful. I can post code, if you really want it.
  #2  
Old 28-Sep-2006, 12:06
Alloishus Alloishus is offline
New Member
 
Join Date: Sep 2006
Posts: 15
Alloishus is on a distinguished road

Re: Windows C++ Problem with Class and Functions


I am going to include a brief synopsis of the code that I used. This is not complete code, and I did not have to run it through the compiler to know that this will not work. I just cannot think of a solution.

Chicken.h file
CPP / C++ / C Code:
class CHICKEN
{
          public :
          TCHAR szName[256] ;
} ;


Child1.File.cpp (with includes...so that I didnt have to do more files here. I do have separate headers with the prototypes et al. in my own code)
CPP / C++ / C Code:
#include "Chicken.h"

CHICKEN egg ;

LRESULT CALLBACK Child1 (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
     static HWND     hwndNameOk, hwndNameEdit ;
     int     iLength ;

     if (hwndNameOk == (HWND) lParam)
     {
          GetWindowText (hwndNameEdit, egg.szName, 256) ; // This saves text from a text box into egg.szName
     }

     return DefWindowProc (hwnd, message, wParam, lParam) ;
}


Child2File.cpp
CPP / C++ / C Code:
#include "Chicken.h"

LRESULT CALLBACK Child2 (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
     switch (message)
	{
	case WM_PAINT :
                     TextOut (hdc, size.cx * 2 - 10, size.cy + 8, egg.szName, lstrlen (egg.szName)) ;
             }
     return DefWindowProc (hwnd, message, wParam, lParam) ;
}

There are alot of elements that I did not include. My code compiles fine if I just want to print text generated from inside of the Child2 function. No problem.

Note that this does will not work simply because Child2 has no idea that the egg in Child1 exists. Of course, I could instantiate an egg inside Child2, but then that has nothing to do with the egg in Child1.

So, although this is windows code, the question is the same for me in C++. How do I get Child2 to see the egg created in Child1 when the function has already been defined (such is the case with <windows.h>)?
  #3  
Old 28-Sep-2006, 15:18
ubergeek ubergeek is offline
Regular Member
 
Join Date: Jan 2005
Posts: 775
ubergeek is a jewel in the roughubergeek is a jewel in the roughubergeek is a jewel in the rough

Re: Windows C++ Problem with Class and Functions


in a header file included by both .cpp files, include
CPP / C++ / C Code:
extern CHICKEN egg;
(Note that of course the CHICKEN class must defined before that statement.) That tells the compiler that there is an instance of CHICKEN somewhere, called egg. Then you put
CPP / C++ / C Code:
CHICKEN egg;
like you have it in any ONE of the .cpp files that include that header, and all the others will see it.
  #4  
Old 28-Sep-2006, 18:34
Alloishus Alloishus is offline
New Member
 
Join Date: Sep 2006
Posts: 15
Alloishus is on a distinguished road

Re: Windows C++ Problem with Class and Functions


Ah,

so you can declare egg to be extern just like a regular variable. I think that is where I was lost. Interestingly, I haven't found any examples (even in my C++ books) that explicitly say that.

Thanks...that should get me going again.
 
 

Recent GIDBlogNot selected for officer school by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Help with class, member functions and consructor GrassPuppet C++ Forum 2 04-Feb-2006 17:34
How many member functions within Thing class? jack223 C++ Forum 4 08-Nov-2005 09:45
Inheritance problem CaptnB C++ Forum 2 27-Jan-2005 08:09
Having problem in calling functions inthe main harsha C Programming Language 1 13-Oct-2004 00:05
help with class functions Mjkramer21 C++ Forum 2 23-Apr-2004 08:23

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 01:46.


vBulletin, Copyright © 2000 - 2010, Jelsoft Enterprises Ltd.