我有相同的代码:
.hpp 文件:
class CConsoleModel
{
char* ParametersBuffer;
...
public:
CConsoleModel() ; // - basic constructor;
~CConsoleModel() ; // -basic destructor
char *DeterminationParameter(std::string _command, int _parametersize);
...
};
.cpp 文件:
char *CConsoleModel::DeterminationParameter(std::string _command, int _parametersize)
{
ParametersBuffer = new char[_parametersize];
unsigned int HexValue;
_command = _command.substr(_command.length() - (_parametersize*2),(_parametersize*2));
//do conversion of the string to the required dimension (_parametrsize):
for (int i(0); i<_parametersize;i++)
{
std::stringstream CommandSteam;
CommandSteam<< std::hex <<_command[2*i];
CommandSteam<< std::hex <<_command[2*i +1];
CommandSteam >> std::hex >> HexValue;
ParametersBuffer[i] = static_cast<char> (HexValue);
}
return ParametersBuffer;
}
程序构建,但运行时崩溃。
如果我改变ParametersBuffer = new char[_parametersize]
到char* ParametersBuffer = new char[_parametersize]
一切正常。我该如何解决这个问题?