1. We have moved from vBulletin to XenForo and you are viewing the site in the middle of the move. Though the functional aspect of everything is working fine, we are still working on other changes including the new design on Xenforo.
    Dismiss Notice

friend function

Discussion in 'C' started by answerme, Oct 16, 2009.

  1. answerme

    answerme New Member

    hye all
    I have class CScenDoc . In that class have function called ONSEND() where I am doing certain operation .I want to call this ONSEND function in other function which is not a member function any class ,its a seperate function. How can i call ONSEND function onto that function .I know through friend function but getting some error

    Code:
    [B]ScenGenDoc .h[/B]
    class CScenGenDoc : public CDocument
    {
    
    public:
    void OnSend();
    }
    Code:
    [B]ScenGenDoc .cpp[/B]
    
    void CScenGenDoc::OnSend()
    {
    //doing certain operation
    }
    Code:
    [B]LAndmovement.cpp[/B]
    double landmove()
    {
    if((strcmp(cursor->LandID,"S1")==0)||(strcmp(cursor->LandID,"s1")==0))
    				{	
    cursor->L_Longitude-=0.05;
    cursor->L_Latitude-=0.001;
    
    OnSend(); //calling onsend function here
    
    }
    thanks in advance
     
  2. xpi0t0s

    xpi0t0s Mentor

    You need a CScenGenDoc object, let's call it x, then you can do x.OnSend();, or if x is a pointer to CScenGenDoc then you would do x->OnSend();

    If you want to call the function without the object existing then you need to make the function static, then it can be called with CScenGenDoc::OnSend();

    You would only need to make landmove() a friend of CScenGenDoc if CScenGenDoc::OnSend() is private or protected. However if that's the case then the designer of CScenGenDoc really didn't mean you to be calling that function directly so you should discuss with the designer how you can access that functionality. If it turns out that CScenGenDoc::OnSend() can be public after all then make it public and don't make landmove() a friend of CScenGenDoc.
     
  3. answerme

    answerme New Member

    Thank for reply
    Since Onsend() function is public in class CScenGenDoc( ScenGenDoc.h) I called CScenGenDoc::OnSend(); in landmovement .cpp file,but its genrating a error

    error C2352: 'CScenGenDoc::OnSend' : illegal call of non-static member function
    I have given header file also #include "ScenGenDoc.h" in landmovement.cpp

    Code:
    [B]Landmovememt.cpp[/B]
    if((strcmp(cursor->LandID,"S1")==0)||(strcmp(cursor->LandID,"s1")==0))
    {	
    	cursor->L_Longitude-=0.05;
    	cursor->L_Latitude-=0.001;
    	if(connected==true)
    	{
    	CScenGenDoc::OnSend();
    	}
    }
    BUT what i find in class CScenGenDoc
    Code:
    class CScenGenDoc : public CDocument
    {
    protected: 
    	CScenGenDoc();
    	DECLARE_DYNCREATE(CScenGenDoc)
    }
    Is it because the class it self is protected.Plz advice me where iam wrong
     
    Last edited: Oct 20, 2009
  4. xpi0t0s

    xpi0t0s Mentor

    > illegal call of non-static member function

    Yes. That's because it's not static and you're trying to call the function without an object.
    Make the function static - as I said in my last post and as the error is telling you - and it will work.
    It's not enough for it to be public; it must be static as well.

    This is because of the way the object oriented methodology works: if an object doesn't exist, then its methods don't exist either.
     

Share This Page