我對我的一個本地C++類中的「this」的NULL值感到困惑。C++中「this」的可能值是多少?
我聲明類Stdafx.h中,像這樣:
extern Filing* Files;
然後實現它stdafx.cpp像這樣:
Filing* Files = new Filing();
調試器扼流圈這種方法,並拋出了「寫訪問衝突「: 消息還說:」這是nullptr「。
void Filing::Startup()
{
this->ExePath = this->GetPathToExe(); //Throws right here
this->LogDirectoryPath = this->GetLogDirectoryPath();
CreateDirectory(LogDirectoryPath->c_str(), NULL);
}
這怎麼可能?我該如何解決?完整的「歸檔」標題和下面的定義。
Filing.h:
#pragma once
#include <Windows.h>
#include <sstream>
using namespace std;
class Filing
{
public:
Filing();
~Filing();
/// <summary>
/// The path to this exe.
/// </summary>
wstring* ExePath;
/// <summary>
/// The path to the log folder that exists or will be created.
/// </summary>
wstring* LogDirectoryPath;
/// <summary>
/// Kicks off some startup logic.
/// </summary>
void Startup();
/// <summary>
/// Determines if the specified directory exists.
/// </summary>
/// <param name="Path"></param>
/// <returns></returns>
bool DoesDirectoryExist(string* Path);
private:
/// <summary>
/// Returns the directory from which this executable is running from.
/// </summary>
wstring* GetPathToExe();
/// <summary>
/// Gets the path to the log directory.
/// </summary>
/// <returns></returns>
wstring* GetLogDirectoryPath();
};
Filing.cpp:
#include "stdafx.h"
#include "Filing.h"
Filing::Filing()
{
this->Startup();
}
Filing::~Filing()
{
delete this->ExePath;
delete this->LogDirectoryPath;
}
void Filing::Startup()
{
this->ExePath = this->GetPathToExe();
this->LogDirectoryPath = this->GetLogDirectoryPath();
CreateDirectory(LogDirectoryPath->c_str(), NULL);
}
bool Filing::DoesDirectoryExist(string* Path)
{
DWORD ftyp = GetFileAttributesA(Path->c_str());
if (ftyp == INVALID_FILE_ATTRIBUTES)
{
Console->WriteLine("Invalid path!");
return false; //something is wrong with your path!
}
if (ftyp & FILE_ATTRIBUTE_DIRECTORY)
{
return true; // this is a directory!
}
return false; // this is not a directory!
}
wstring* Filing::GetPathToExe()
{
#ifdef UNICODE
TCHAR ownPth[260];
#else
char ownPth[MAX_Path];
#endif // UNICODE
// Will contain exe path
HMODULE hModule = GetModuleHandle(NULL);
if (hModule != NULL)
{
// When passing NULL to GetModuleHandle, it returns handle of exe itself
GetModuleFileName(hModule, ownPth, (sizeof(ownPth)));
return new wstring(ownPth);
}
else
{
throw new exception("Error! NullPointerException!");
}
}
wstring* Filing::GetLogDirectoryPath()
{
//if (this == nullptr)
//{
// int i = 0;
//}
wstring *directory;
const size_t last_slash_idx = ExePath->rfind('\\');
if (string::npos != last_slash_idx)
{
directory = new wstring(ExePath->substr(0, last_slash_idx));
directory->append(L"\\Logs");
}
else
{
throw new exception("Error! Directory not found from path!");
}
return directory;
}
事情我至今嘗試過:
清理和重建。
初始化歸檔。
編輯:
我所看到的關於不使用的stdafx.h和stdafx.cpp什麼我使用它的一些意見。我現在正在將它們用於全局變量/類。我怎麼能在其他地方定義它們,仍然是全球性的?
EDIT2:
究其根源,錯誤一路回了一點GETTIME()函數。
const char* Writer::GetTime()
{
string* Formatted = new string("[");
time_t rawtime;
tm* timeinfo;
char buffer[80];
time(&rawtime);
timeinfo = std::localtime(&rawtime);
strftime(buffer, 80, "%Y-%m-%d-%H-%M-%S", timeinfo);
Formatted->append(buffer);
Formatted->append("]: ");
const char* Ret = Formatted->c_str();
delete Formatted;
delete timeinfo;//Errors here SOMETIMES. Any ideas?
return Ret;
}
我不在Winders中編程,但是你應該編輯stdafx.h?我以爲這是編譯器生成的預編譯頭文件?但是,那麼我對Winders有什麼瞭解? – systemcpro
@systemcpro:它由IDE生成。但你可以自由編輯它。 –
爲什麼這個== nullptr不允許?這不是在我的原始代碼中的錯誤,它只是調試代碼,以證明錯誤沒有被調試器誤診。無論如何,如果你刪除它,你仍然會遇到問題。 – SneakyTactician2