C++ c++;类型为x的参数与类型为atexit的参数不兼容

C++ c++;类型为x的参数与类型为atexit的参数不兼容,c++,std,C++,Std,当我尝试在std中运行带有theatexti()的函数时,出现了这个错误。我不理解这个错误 以下是错误: IntelliSense: argument of type "void (Demo3Main::*)()" is incompatible with parameter of type "void (__cdecl *)()" 代码如下: Demo3Main::Demo3Main(void) : BaseEngine( 50 ) { atexit(RestorScore); }

当我尝试在std中运行带有theatexti()的函数时,出现了这个错误。我不理解这个错误

以下是错误:

 IntelliSense: argument of type "void (Demo3Main::*)()" is incompatible with parameter of type "void (__cdecl *)()"
代码如下:

Demo3Main::Demo3Main(void)
: BaseEngine( 50 )
{
    atexit(RestorScore);
}

void Demo3Main::RestorScore(){
    std::ofstream outfile("old_score.txt");
    int num1 = 0;
    outfile << num1;
    outfile.close();
}
Demo3Main::Demo3Main(无效)
:基本发动机(50)
{
atexit(恢复分数);
}
void Demo3Main::RestorScore(){
标准:流出管(“old_score.txt”);
int num1=0;

outfileELI5版本

atexit回调的规范是

void (*function)(void)
这意味着您必须在表单上传递函数:

void my_function()
{
}
但是,您传递了一个具有不同类型的类成员函数。其类型为:

 void (Demo3Main::*)()
通过在类外编写函数,可以使程序正常工作。例如:

void RestorScore(){
   std::ofstream outfile("old_score.txt");
   int num1 = 0;
   outfile << num1;
}
atexit(RestoreScore);
void RestorScore(){
标准:流出管(“old_score.txt”);
int num1=0;

outfile您正在传递成员函数,但atexit需要自由函数?嗯,我不知道这意味着什么,我如何解决这个问题?您可能应该将数据保存在析构函数中,而不是作为退出处理程序。使类成员函数
静态
也应该可以工作。很好。我忘记了这一点。但是,这会使语法复杂化x表示这个简单的情况。