我尝试编写一个文件管理器的 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