Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/72.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ C++;类终止_C++_Class_Events - Fatal编程技术网

C++ C++;类终止

C++ C++;类终止,c++,class,events,C++,Class,Events,我已经声明了一个类,并在其中实例化了一个类,并期望它启动 ~CLog(); 但出于某种原因,它没有。有没有人看到任何明显的错误,为什么会发生这种情况 我声明该类在一个结束的空白区域内,所以我认为它应该被触发。 我没有明确地销毁该类,但我只是希望它自动超出范围并终止 #pragma once #include <fstream> using namespace std; class CLog { public: CLog(wstring filename);

我已经声明了一个类,并在其中实例化了一个类,并期望它启动

~CLog();
但出于某种原因,它没有。有没有人看到任何明显的错误,为什么会发生这种情况

我声明该类在一个结束的空白区域内,所以我认为它应该被触发。 我没有明确地销毁该类,但我只是希望它自动超出范围并终止

#pragma once
#include <fstream>
using namespace std;

class CLog 
{
  public:
    CLog(wstring filename);
    ~CLog();
    void WriteString(wstring uString);
  private:
    wofstream m_stream;
    wstring m_sPath;
};

#include "log.h";
#include "strhelper.h"

#include <fstream>
#include <iostream>
#include <string>
#include <locale>
#include <codecvt>

wstring m_sText=L"";
wstring m_sPath=L"";

CLog::CLog(wstring uPath) 
{
    m_sPath=uPath;
}
void CLog::WriteString(wstring uString)
{
    m_sText+=uString;
    m_sText+=L"\n";
}
CLog::~CLog()
{

    if (FileExists(m_sPath))
    {
        DeleteFile(m_sPath);
    }

    //open for appending
    m_stream.imbue(std::locale(std::locale::empty(), new std::codecvt_utf8<wchar_t,0x10ffff,std::generate_header>));
    m_stream.open(m_sPath,fstream::in | fstream::out | fstream::app); 

    m_stream << m_sText.c_str();

    m_stream.close();
}

我正在使用VC2010。

您正在动态实例化
CLog
。在这种情况下,您需要显式删除它。
如果您在堆栈
阻塞日志(sLogPath)
上创建它,则当对象超出范围时,将调用析构函数。

您正在动态实例化
阻塞
。在这种情况下,您需要显式删除它。
如果您在堆栈
Clog日志(sLogPath)
上创建它,当对象超出范围时将调用析构函数。

请添加行如何实例化
Clog
向我们显示您使用
Clog
~Clog()的代码
仅当变量超出范围IIRCWhat is
我在结束的void中声明了类
?void foo(){wstring sLogPath;sLogPath=GetSpecialFolderDesktop()+L“\\load.log”;CLog*pLog=new CLog(sLogPath);pLog->WriteString(L“Something”);}请添加行您如何实例化
CLog
向我们显示您正在使用的代码
CLog
~CLog()
仅在变量超出范围时运行IIRCWhat is
我在结束的void中声明了类
?void foo(){wstring sLogPath;sLogPath=GetSpecialFolderDesktop()+L”\\load.log“CLog*pLog=new CLog(sLogPath);pLog->WriteString(L“Something”);}虽然OP最初没有发布相关代码,但完全可以预见这就是原因。尽管OP最初没有发布相关代码,但完全可以预见这就是原因。
void foo() {
  wstring sLogPath;
  sLogPath=GetSpecialFolderDesktop() + L"\\load.log";
  CLog *pLog = new CLog(sLogPath);
  pLog->WriteString(L"Something);
}