C++错误 C2352:'CSchedulerDlg::Select':非法调用非静态成员函数

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

本文关键字:调用 静态成员 函数 非法 Select 错误 C++ CSchedulerDlg C2352      更新时间:2023-10-16

我正在尝试修改和改进C++中的作业调度程序应用程序

许多成员函数被声明为static,因此不能作用于非静态成员变量。

当试图向类添加附加功能时,会出现问题。特别是,我想知道是否可以在静态成员函数的定义中调用非静态成员函数。

也就是说,假设我们有成员函数声明:

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

我想从email函数的实现中调用Select函数。但我得到了一个错误:

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

这个错误是有道理的,因为静态成员函数不能作用于当前对象,但我仍然需要从email函数中执行Select函数。是否存在变通方法?

导致错误的相关代码是:

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

其中CCD_ 6是类内的私有成员变量。

为什么不能简单地为Select函数提供一个对象?

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.
}

唯一的解决方案是使email非静态,或者将参数CSchedulerDlg&添加到email:

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

并在CCD_ 12对象上调用CCD_。两种解决方案非常相似。