0

I am attempting to modify and improve a job scheduler application in C++

Many of the member functions are declared as static, and hence cannot act on the non-static member variables.

The problem arises when attempting to add additional functionality to the class. In particular, I was wondering if it was possible to call a non-static member function inside the definition of a static member function.

That is, suppose we have the member functions declarations:

static void email(CString message);
CRecordset * Select(CString SQL, CDatabase* dataBase);

I would like to call the Selectfunction from within the implementation of the email function. But I get an error:

error C2352: 'CSchedulerDlg::Select' : illegal call of non-static member function

The error makes sense because static member functions cannot act upon the current object, but I still need to perform the Select function from within the email function. Does there exist a work around method?

The relevant code that causes the error is:

void CSchedulerDlg::email(CString message)
{
    CRecordset * emails = Select("some SQL query", db);
}

where static CDatabase* dbis a private member variable within the class.

Ryan J. Shrott
  • 592
  • 1
  • 8
  • 26
  • You did not actually show the line of code (and the relevant lines nearby) that is generating the error. Include the code that causes the error. – abelenky Jan 12 '16 at 18:58
  • @abelenky I added this – Ryan J. Shrott Jan 12 '16 at 19:02
  • There are probably better links out there. But this is sounding like you want something similar to a singleton. Or some static function that creates any number of instances so you can call member functions from within static member functions. http://stackoverflow.com/questions/18997240/how-does-getinstance-work. – Matthew Hoggan Jan 12 '16 at 19:05
  • If `db` is static, why is `Select` nonstatic? – Dan Jan 12 '16 at 19:06
  • @Dan I have tried this, but it gives me other error: error LNK2001: unresolved external symbol "public: static class CDatabase * CSchedulerDlg::db" – Ryan J. Shrott Jan 12 '16 at 19:24

2 Answers2

0

Why can you not simply provide an object for the Select function?

void CSchedulerDlg::email(CString message)
{
    CSchedulerDlg aDlg;                                      // Now you have an object.

    CRecordset * emails = aDlg.Select("some SQL query", db); // This is now a valid call.
}
abelenky
  • 63,815
  • 23
  • 109
  • 159
0

The only work around is to either make email non static, or add parameter CSchedulerDlg& to email:

 static void email(CSchedulerDlg& dlg, CString message);

and call select on dlg object. Both solutions are quite similar.

marcinj
  • 48,511
  • 9
  • 79
  • 100