我從這段代碼中得到一個無效的空指針錯誤。我猜這是與字符串有關的事情,但正如我最近才瞭解到的那樣,我找不到問題。空指針無效 - C++
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);
}
這裏是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();
}
}
預先感謝任何幫助。編輯:錯誤發生在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();
}
}
希望這是正確的東西。
哪一行發生錯誤?顯示堆棧跟蹤的頂部是 – Chris
完全沒用。什麼是始發線? –
請顯示整個堆棧跟蹤。我們需要知道什麼叫'_CrtDbgBreak()',什麼叫_that_函數,等等。 –