-1 c++ null-pointer
我从这部分代码中收到无效的空指针错误。我猜这与字符串有关,但由于我最近才了解它们,我找不到问题所在。
string batchCipherFiles()
{
int count(0);
string text, file;
ifstream inputFile;
cout << " How many files do you wish to encrypt?: ";
cin >> count;
for (int i(1) ; i <= count ; ++i)
{
stringstream ss;
ss << "unencrypted" << i << ".txt";
file = ss.str();
inputFile.open(file.c_str(), ios::in);
if (inputFile.fail())
{
cout << "\n An error has occurred.";
}
else
{
while (!inputFile.eof())
{
getline(inputFile, text);
cout << "\n " << file << " = " << text << "\n\n";
int applyCeasarShift(string,string);
applyCeasarShift(text, file);
}
inputFile.close();
}
}
return (0);
}
Run Code Online (Sandbox Code Playgroud)
这是来自 xstring 的调试行:
#ifdef _DEBUG
_CRTIMP2_PURE void __CLRCALL_PURE_OR_CDECL _Debug_message(const wchar_t *message, const wchar_t *file, unsigned int line)
{ // report error and die
if(::_CrtDbgReportW(_CRT_ASSERT, file, line, NULL, message)==1)
{
::_CrtDbgBreak();
}
}
Run Code Online (Sandbox Code Playgroud)
在此先感谢您的帮助。
编辑:错误发生在 Return 语句上,在 xstring 的摘录中,黄色箭头指向“::_CrtDbgBreak();”
编辑2:
xstring:
basic_string(const _Elem *_Ptr)
: _Mybase()
{ // construct from [_Ptr, <null>)
_Tidy();
assign(_Ptr);
}
xutility:
template<class _Ty> inline
void _Debug_pointer(const _Ty *_First, _Dbfile_t _File, _Dbline_t _Line)
{ // test iterator for non-singularity, const pointers
if (_First == 0)
_DEBUG_ERROR2("invalid null pointer", _File, _Line);
}
stdthrow:
#ifdef _DEBUG
_CRTIMP2_PURE void __CLRCALL_PURE_OR_CDECL _Debug_message(const wchar_t *message, const wchar_t *file, unsigned int line)
{ // report error and die
if(::_CrtDbgReportW(_CRT_ASSERT, file, line, NULL, message)==1)
{
::_CrtDbgBreak();
}
}
Run Code Online (Sandbox Code Playgroud)
希望这是正确的东西。
问题是这string是返回类型batchCipherFiles但return (0);正在返回:更改返回类型或返回 a string。
我认为return (0);将被隐式转换为std::string((char*)0);,从而导致崩溃。std::string构造函数的文档指出:
使用 s 指向的以空字符结尾的字符串的内容构造字符串。字符串的长度由第一个空字符决定。s 不能是 NULL 指针。