C++错误 C2352:'CSchedulerDlg::Select':非法调用非静态成员函数
C++ error C2352: 'CSchedulerDlg::Select' : illegal call of non-static member function
我正在尝试修改和改进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_。两种解决方案非常相似。
相关文章:
- 在 nullptr 上调用无状态类的非静态成员函数是否合法?
- 非静态成员失败的线程调用函数
- 处理类内的回调时,必须调用对非静态成员函数的引用
- LNK2019 在文件中调用静态成员的方法时出错.cpp
- 为什么我不能像这样在静态成员函数中调用静态成员变量?
- 当我在C++中调用 struce 的只读静态成员时,线程是否安全
- 简化静态成员函数调用的方法
- 通过指针调用静态成员函数
- Visual 2015 C++编译器:使用对象调用静态成员不会导致编译器错误
- 在不存在的对象上调用静态成员函数
- 在静态成员变量初始值设定项中,为什么调用静态成员函数(而不是全局函数)
- 为什么在包含类的构造函数之前调用静态成员的构造函数
- 编译递归模板以调用静态成员函数时类型不完整
- 调用静态成员函数会导致运行时错误
- 在c++中调用静态成员的方法进行初始化
- 是在第一次调用静态成员函数之前初始化的静态类变量
- C++:在不同类的派生对象的基指针上调用静态成员函数
- 从继承的类实例调用静态成员
- 在对象上调用静态成员函数——有没有办法将其转化为编译器错误
- 调用c++静态成员函数的Objective-c包装器