friend Class Help

This is a discussion on friend Class Help within the C++ Programming forums, part of the General Programming Boards category; Hi Im making a gui, in c\c++ mixed (hehe). The problem m having is i need to call a function ...

  1. #1
    Registered User
    Join Date
    Jan 2002
    Posts
    4

    Question friend Class Help

    Hi
    Im making a gui, in c\c++ mixed (hehe).
    The problem m having is i need to call a function from another class before that class is actually defined.
    My WindowClass is defined first and needs to call TaskBarObj.Add(WinID) from the TaskBarClass which is defined AFTER the WindowClass.

    eg...very cut down and off the top of my head

    Code:
    //window.cpp  
    class WindowClass
    {
        private:
                    int WinID;
        public:
                    WindowClass(){};
                    ~WindowClass();
                    
                    void Window(int WinID);  //Constructor 
                    void Close(int WinID);
                    void Minimize(int WinID);
                    void Maximize(int WinID);
    };
    
    //make pointer to 100 WindowClass obj on free store
    WindowClass *WinArray=new WindowClass[100];  
    
    //Make WindowClass Obj to call WindowClass functions ect
    WindowClass WindowObj;
    
    //Array Offset counter
    int WinCounter=-1;
    
    void WindowClass::Window(int WinID)
    {
        WinCounter++;
        WinArray[WinCounter].WinID=WinID;
    
       //needs to call TaskBar Class   TaskbarObj.Add(WinID);
       //But it has not been defined ....will making TaskBask a frined class enable it to call a undefined function???  
    
    }
    
    
    
    //TaskBar.cpp
    
    class TaskBarClass
    {
      private:
                   int TaskBarID  
                   
       public:
                  TaskBarClass(){};
                  ~TaskBarClass();
     
                  void Add(int WinID);
                  void Close(int TaskBarID);
                  void Minimize(int TaskBarID);
                  void Maximize(int TaskBarID);
    };
    
    //Make pointer to 100 TaskBarClass Objs on free store
    TaskBarClass *TaskBarArray=new TaskBarClass[100];
    
    //Make TaskBarClass Obj to call TaskBar functions etc.
    TaskBarClass TaskBarObj;
    
    //Array Offset counter
    int TaskBarCounter=-1;
    
    
    void TaskBarClass::Add(int WinID)
    {
       
       //Window.cpp needs to call this when it creates a new window
      
      TaskBarCounter++;
       TaskBarArray[TaskBarCounter].TaskBarID=WinID;
      
    }
    
    
    void TaskBarClass::Minimize(int TaskBarID)
    {
       //minimize a window through taskbar right click menu 
      WindowObj.Minimize(TaskBarID);
     
    
    }
    So the problem is that i can declare one class, then the second class, but the first class wont be able to call functions in the second class because it was defined first.
    will making taskbarclass a frined of windowclass allow me to call a a taskbarclass function within windowclass, before the taskbarclass is actually defined???

    can anyone help??

    plz email me as i dont know when i will be able to check this board again.....if you need source plz ask and i will send you the two seperate cpp class files
    email: RealitySoftware@hotmail.com


    Thx.....

    Code tags added by kermi3

  2. #2
    Lead Moderator kermi3's Avatar
    Join Date
    Aug 1998
    Posts
    2,595
    I am posting this because you did not use code tags on this thread. In the furture please use Code Tags. They make your code MUCH easier to read and people will be much more likely to help you if you do. And they'll be happy about helping you


    For example:

    Without code tags:

    for(int i=0;i<5;i++)
    {
    cout << "No code tags are bad";
    }

    With Code Tags:
    Code:
    for(int i=0;i<5;i++)
    {
         cout << "This code is easy to read";
    }
    This is of course a basic example...more complicated code is even easier to read with code tags than without.

    I've added code tags for you this time. They can be added by putting [code] at the beginning of your code and [/code] at the end. More information on code tags may be found at the link in my signature. I also suggest you take a look at the board guildlines if you have not done so already. Any further questions or ways I can help please feel free to PM me.

    This is a common first post mistake, just remember to use [code] tags in the future and you'll get much more help.

    Good Luck,
    Kermi3
    Kermi3

    If you're new to the boards, welcome and reading this will help you get started.
    Information on code tags may be found here

    - Sandlot is the highest form of sport.

  3. #3
    Registered User
    Join Date
    Jan 2002
    Posts
    4

    Talking

    Thx
    Will remember to use the [code] tags in future....
    Any help with my problem tho?

    Thx anyways...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 39
    Last Post: 01-18-2009, 10:53 PM
  2. Problem with friend class declaration in a namespace
    By Angus in forum C++ Programming
    Replies: 2
    Last Post: 12-09-2008, 12:29 PM
  3. deriving classes
    By l2u in forum C++ Programming
    Replies: 12
    Last Post: 01-15-2007, 04:01 PM
  4. Need help to build network class
    By weeb0 in forum C++ Programming
    Replies: 0
    Last Post: 02-01-2006, 10:33 AM
  5. structure vs class
    By sana in forum C++ Programming
    Replies: 13
    Last Post: 12-02-2002, 06:18 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21