[英]How to write a C++ program on a mac that compiles and runs C++ code in a separate text file
[英]c++ program of file manager compiles, but does not works
我嘗試編寫一個文件管理器的c ++程序。 我寫了一個“CommandEngine”類,它處理命令,一個“Command”類,它是抽象類,包含“執行”函數等。我嘗試在指定的路徑上創建一個文件(沒有在該級別的開發中給出響應)我的節目)。 我編寫的代碼編譯成功,但是當我嘗試執行它時,我遇到了錯誤
“FileManager2.exe中0x0022DC96處的未處理異常:0xC0000005:訪問沖突讀取地址0x00000014。”
我將非常感謝任何幫助。 感謝大家。
// FileManager.cpp
#include <stdio.h>
#include <tchar.h>
#include "CommandEngine.h"
#include "Command.h"
#include "CreateFile.h"
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
CommandEngine C;
C.CommandHandler();
return 0;
}
// CommandEngine.h
#ifndef COMMANDENGINE_H
#define COMMANDENGINE_H
#include "Command.h"
#include "CreateFile.h"
#include "Input.h"
#include <map>
#include <string>
using namespace std;
class CommandEngine
{
public:
typedef map< string , Command * > MapOfHandlers;
MapOfHandlers CommandHandlers;
Input * input;
Command * GetCommand(const string & commandName)
{
map< string , Command * >::iterator iter;
iter = CommandHandlers.find(commandName);
return iter->second;
};
void CommandHandler();
CommandEngine();
~CommandEngine();
};
CommandEngine::CommandEngine()
{
CreateFileCl * Cr;
string s = "create";
CommandHandlers.insert(pair<string, Command *>(s, Cr));
}
CommandEngine::~CommandEngine()
{
}
void CommandEngine::CommandHandler()
{
Response response;
Command * command = GetCommand( ( input->ReadInput() ) -> GetCommandName());
command->Execute(input, &response);
WriteResponse(&response);
}
#endif // COMMANDENGINE_H
//Command.h
#ifndef COMMAND_H
#define COMMAND_H
#include "Input.h"
#include "Response.h"
using namespace std;
class Command
{ public:
virtual void Execute(Input * input, Response * response ) = 0;
};
#endif // Command_H
/*void StrToChar(string s)
{
string s;
string writable = new char[str.size() + 1];
std::copy(str.begin(), str.end(), writable);
writable[str.size()] = '\0'; // don't forget the terminating 0
// don't forget to free the string after finished using it
delete[] writable;
} */
////////////////////////////////////////////////////////////////////////////////////////////////////
// Input.h
#ifndef INPUT_H
#define INPUT_H
#include <string>
#include <iostream>
using namespace std;
class Input
{
public:
string CommandName;
string FileName;
string DestinationPath; // For " copy " command
Input * ReadInput();
const string GetCommandName()// can be no useful
{
return CommandName;
};
Input();
~Input();
};
Input::Input()
{
CommandName = FileName = DestinationPath = " ";
}
Input::~Input()
{
}
Input * Input::ReadInput()
{
cout<<"Enter command";
getline(cin,CommandName);
getline(cin, FileName);
getline(cin,DestinationPath );
return this;
}
#endif // INPUT_H
// CreateFile.h
#ifndef CREATEFILE_H
#define CREATEFILE_H
#include <windows.h>
#include "Command.h"
using namespace std;
class CreateFileCl : public Command
{
public:
virtual void Execute(Input * input, Response * response );
};
void CreateFileCl::Execute(Input * input, Response * response )
{
/*const string text = (input->FileName).c_str();
wchar_t wtext[20];
mbstowcs(wtext, text, strlen(text)+1);//Plus null
LPWSTR ptr = wtext; */
CreateFileA( (input->FileName).c_str(), 0, 0, 0, 0, 0, 0);
}
#endif // CREATEFILE_H
在CommandEngine
構造函數中,您將在CommandHandlers
映射中存儲未初始化的CreateFileCl
指針。 當您稍后嘗試通過該指針調用Command::Execute
時,會發生可怕的事情(未定義的行為)。
您有許多未初始化的對象可能會導致代碼崩潰。
首先,您不要在CommandEngine類中初始化輸入 ,因此當您調用input->ReadInput()
您使用的是未初始化的指針。
其次,正如Casey所說,你沒有初始化你在CommandHandlers列表中插入的CreateFileCl對象,因此當你嘗試執行也將失敗的命令時。
您可以通過更新CommandEngine構造函數來初始化這兩個對象來解決此問題。
CommandEngine::CommandEngine()
{
input = new Input();
CreateFileCl * Cr = new CreateFileCl();
string s = "create";
CommandHandlers.insert(pair<string, Command *>(s, Cr));
}
請注意,您還需要刪除CommandEngine析構函數中的輸入對象。
CommandEngine::~CommandEngine()
{
delete input;
}
釋放CreateFileCl對象的內存更復雜 - 假設可能有多個命令處理程序,因此您需要遍歷列表並刪除所有這些。 但實際上你不應該像這樣分配內存。 理想情況下,您應該使用智能指針來處理內存管理。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.