Go4Expert

Go4Expert (http://www.go4expert.com/)
-   C (http://www.go4expert.com/forums/c/)
-   -   friend function (http://www.go4expert.com/forums/friend-function-t19777/)

answerme 16Oct2009 11:10

friend function
 
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:

ScenGenDoc .h
class CScenGenDoc : public CDocument
{

public:
void OnSend();
}

Code:

ScenGenDoc .cpp

void CScenGenDoc::OnSend()
{
//doing certain operation
}

Code:

LAndmovement.cpp
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

xpi0t0s 17Oct2009 04:09

Re: friend function
 
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.

answerme 20Oct2009 10:41

Re: friend function
 
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:

Landmovememt.cpp
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

xpi0t0s 20Oct2009 12:21

Re: friend function
 
> 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.


All times are GMT +5.5. The time now is 00:39.