Click to See Complete Forum and Search --> : How to call a member function of a class from a member function of another class


Crimean
July 1st, 1999, 11:29 AM
I have problems when calling a member function of a class ( if i do not define this function as member i cannot use the pointer for the recordset) within another class
CResultDlg is a dialog class where i call by pressing a button and using .DoModal(); And also CNewWordsView::find_random_record(); phrase gets error. I searched help but could not find how to call a member function from another class.
Thank you.


#My programs name is NewWords and i have created it with App.Wizard
#in NewWordsView.cpp
void CNewWordsView::find_random_record()
{
srand( (unsigned)time( NULL ) );
int random_num=rand();
m_pSet->MoveFirst();
m_pSet->MoveLast();
int max_record=m_pSet->GetRecordCount();
m_pSet->MoveFirst();
int decimal, sign;
char *buffer;
double result;
result=(random_num/32500)*max_record;
buffer = _fcvt( result, 7, &decimal, &sign );
int record_pos=atoi(buffer);
if (record_pos!=(max_record-1))
{ m_pSet->Move(record_pos);
}
else
{
m_pSet->Move(record_pos-1);
}

}


void CResultDlg::OnBtn_Vote()
{
CNewWordsView::find_random_record();
}

chiuyan
July 1st, 1999, 11:35 AM
to call the function like
CNewWordsView::find_random_record();
from anywhere in the program, CNewWordsView::find_random_record would have to be defined static, but that would make you unable to use your m_pSet member variable. For the function to be called as written, you need an instance of your CNewWordsView class that your dialog can use to call the function find_random_record. Do you have an instance of that class around when you call DoModal? Perhaps you can pass it in to your CResultDlg class, or access it via GetParent () from within your CResultDlg::OnBtn_Vote() function.

If you could post more of the code, or explain the surrounding situation a little better, perhaps I (or someone else) can be more helpful.

HTH
--michael